Skip to content

Stdlib: strcmp()/strncmp() null operands — TypeError not empty-string coercion (ext/standard/string.c) #11261

Description

@PurHur

Category

stdlib

Problem

Zend coerces null string operands to "" for internal string-compare builtins (strcmp, strncmp, etc.) and returns an integer compare result. This compiler’s VM raises TypeError on null, changing observable behavior for legacy code and php-src-strict parity.

Distinct from #4346 (array/object operands must TypeError) and #8825 (enum case operands must TypeError).

php-src reference

Repro

<?php
echo strcmp(null, 'a'), "\n";
echo strncmp(null, 'a', 1), "\n";
echo strcmp('a', null), "\n";

Committed: test/repro/maintainer_gap_strcmp_null_coerce.php

./script/docker-exec.sh -- bash -lc '
  source script/php-env.sh
  php test/repro/maintainer_gap_strcmp_null_coerce.php
  php bin/vm.php test/repro/maintainer_gap_strcmp_null_coerce.php
'
Call Zend PHP 8.2 This compiler VM
strcmp(null, 'a') -1 TypeError
strncmp(null, 'a', 1) -1 TypeError
strcmp('a', null) 1 (fatal on first null arg in script)

Scope (PHP-in-PHP)

Layer Path
VM ext/standard/strcmp.php, strncmp.php, shared VmString::coerceStringBuiltinArg guards
JIT/AOT matching lib/JIT/Builtin/ handlers
Tests test/compliance/cases/stdlib/strcmp_null_coerce.phpt

Done when (php-src-strict)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions