Skip to content

Stdlib: strcmp family — TypeError for array/object operands (ext/standard/string.c parity) #4346

Description

@PurHur

Category

stdlib

Problem

strcmp(), strcasecmp(), strncmp(), and strncasecmp() are typed string in PHP 8+. Passing array or object operands must throw TypeError with Zend message shapes. This compiler’s VM paths either compare incorrectly (strcmp([], [])0) or return sentinel integers (strcasecmp([], 'x')-1) instead of failing.

php-src reference

Repro (failure today)

<?php
declare(strict_types=1);

try {
    strcmp([], 'x');
} catch (Throwable $e) {
    echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_export(strcmp([], []));
echo "\n";
var_export(strcasecmp([], 'x'));
echo "\n";
HARNESS_DOCKER_RUN_OPTS='--memory=8g --cpus=2' ./script/docker-exec.sh -- bash -lc '
  source script/php-env.sh
  php repro.php
  php bin/vm.php repro.php
'
Call Zend PHP 8.x This compiler VM (today)
strcmp([], 'x') TypeError TypeError (OK)
strcmp([], []) TypeError 0
strcasecmp([], 'x') TypeError -1

Scope (this repo)

Module Path
VM ext/standard/strcmp.php, strcasecmp.php, strncmp.php, strncasecmp.php
JIT/AOT lib/JIT/Builtin/ counterparts
Tests compliance PHPT + test/unit/*Compare*

Done when

  • Non-string operands (array/object/resource where disallowed) raise TypeError, never silent compare or LogicException.
  • No false 0 / -1 results for invalid types.

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 apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions