Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions ext/standard/VmString.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,16 +807,26 @@ public static function strncmp(string $a, string $b, int $length): int
}
$lenA = self::byteLength($a);
$lenB = self::byteLength($b);
$compare = $length;
if ($compare > $lenA) {
$compare = $lenA;
}
if ($compare > $lenB) {
$compare = $lenB;
}
for ($i = 0; $i < $compare; ++$i) {
$ordA = self::byteOrd($a[$i]);
$ordB = self::byteOrd($b[$i]);
for ($i = 0; $i < $length; ++$i) {
if ($i >= $lenA) {
if ($i >= $lenB) {
return 0;
}
if (0 === $lenA) {
return -1;
}
$ordA = 0;
$ordB = self::byteOrd($b[$i]);
} elseif ($i >= $lenB) {
if (0 === $lenB) {
return 1;
}
$ordA = self::byteOrd($a[$i]);
$ordB = 0;
} else {
$ordA = self::byteOrd($a[$i]);
$ordB = self::byteOrd($b[$i]);
}
if ($ordA !== $ordB) {
return $ordA - $ordB;
}
Expand Down
23 changes: 19 additions & 4 deletions ext/standard/strcmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPCompiler\JIT\JitStringBuiltinArg;
use PHPCompiler\JIT\JitStringCompare;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\InternalStrictArg;
use PHPLLVM\Value;

final class strcmp extends Internal
Expand All @@ -26,8 +27,8 @@ public function execute(Frame $frame): void
if (2 !== count($frame->calledArgs)) {
throw new \LogicException('strcmp() requires exactly two arguments');
}
$a = VmString::requireStringBuiltinArg($frame->calledArgs[0], 'strcmp', 0, 'string1');
$b = VmString::requireStringBuiltinArg($frame->calledArgs[1], 'strcmp', 1, 'string2');
$a = self::vmStringArg($frame, 0, 'string1');
$b = self::vmStringArg($frame, 1, 'string2');
if (null === $frame->returnVar) {
return;
}
Expand All @@ -42,9 +43,23 @@ public function call(Context $context, JITVariable ...$args): Value
if (2 !== count($args)) {
throw new \LogicException('strcmp() requires exactly two arguments');
}
$left = JitStringBuiltinArg::lowerRequiredString($context, $args[0], 'strcmp', 0, 'string1');
$right = JitStringBuiltinArg::lowerRequiredString($context, $args[1], 'strcmp', 1, 'string2');
$left = JitStringBuiltinArg::lower($context, $args[0], 'strcmp', 0, 'string1');
$right = JitStringBuiltinArg::lower($context, $args[1], 'strcmp', 1, 'string2');

return JitStringCompare::strcmp($context, $left, $right);
}

private static function vmStringArg(Frame $frame, int $argIndex, string $paramName): string
{
if (InternalStrictArg::isCallerStrict($frame)) {
return InternalStrictArg::requireString($frame, $argIndex, 'strcmp', $paramName)->toString();
}

return VmString::coerceStringBuiltinArgNoObject(
$frame->calledArgs[$argIndex],
'strcmp',
$argIndex,
$paramName
);
}
}
23 changes: 19 additions & 4 deletions ext/standard/strncmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPCompiler\JIT\JitLongArg;
use PHPCompiler\JIT\JitStringBuiltinArg;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\InternalStrictArg;
use PHPLLVM\Value;

/**
Expand All @@ -27,8 +28,8 @@ final class strncmp extends Internal
public function execute(Frame $frame): void
{
$this->requireExactArgCount($frame, 'strncmp', 3);
$a = VmString::requireStringBuiltinArg($frame->calledArgs[0], 'strncmp', 0, 'string1');
$b = VmString::requireStringBuiltinArg($frame->calledArgs[1], 'strncmp', 1, 'string2');
$a = self::vmStringArg($frame, 0, 'string1');
$b = self::vmStringArg($frame, 1, 'string2');
if (null === $frame->returnVar) {
return;
}
Expand All @@ -44,8 +45,8 @@ public function call(Context $context, JITVariable ...$args): Value
if (!$this->requireExactJitArgCount($context, $args, 'strncmp', 3)) {
return $context->getTypeFromString('int64')->constInt(0, false);
}
$p0 = $this->stringDataPtr($context, JitStringBuiltinArg::lowerRequiredString($context, $args[0], 'strncmp', 0, 'string1'));
$p1 = $this->stringDataPtr($context, JitStringBuiltinArg::lowerRequiredString($context, $args[1], 'strncmp', 1, 'string2'));
$p0 = $this->stringDataPtr($context, JitStringBuiltinArg::lower($context, $args[0], 'strncmp', 0, 'string1'));
$p1 = $this->stringDataPtr($context, JitStringBuiltinArg::lower($context, $args[1], 'strncmp', 1, 'string2'));
$length = $context->builder->zExt(
$context->builder->trunc(
JitLongArg::lower($context, $args[2], 'strncmp() length'),
Expand All @@ -58,4 +59,18 @@ public function call(Context $context, JITVariable ...$args): Value

return $context->builder->sExt($raw, $i64);
}

private static function vmStringArg(Frame $frame, int $argIndex, string $paramName): string
{
if (InternalStrictArg::isCallerStrict($frame)) {
return InternalStrictArg::requireString($frame, $argIndex, 'strncmp', $paramName)->toString();
}

return VmString::coerceStringBuiltinArgNoObject(
$frame->calledArgs[$argIndex],
'strncmp',
$argIndex,
$paramName
);
}
}
11 changes: 11 additions & 0 deletions test/compliance/cases/stdlib/strcmp_null_coerce.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
stdlib strcmp()/strncmp() null operands coerce without strict_types (#11261, ext/standard/string.c)
--FILE--
<?php
echo strcmp(null, 'a'), "\n";
echo strncmp(null, 'a', 1), "\n";
echo strcmp('a', null), "\n";
--EXPECT--
-1
-1
1
5 changes: 5 additions & 0 deletions test/repro/maintainer_gap_strcmp_null_coerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

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