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
35 changes: 22 additions & 13 deletions ext/types/JitStrlen.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ final class JitStrlen
public static function lowerLength(Context $context, JITVariable $arg): Value
{
if (JITVariable::TYPE_NULL === $arg->type || $arg->isNullConstant) {
self::emitTypeErrorAndAbort($context, 'null');
if ($context->callerStrictTypes) {
self::emitTypeErrorAndAbort($context, 'null');

return $context->getTypeFromString('int64')->constInt(0, false);
}

return $context->getTypeFromString('int64')->constInt(0, false);
}
Expand Down Expand Up @@ -123,20 +127,25 @@ private static function lowerLengthFromValueBox(Context $context, JITVariable $a
);
$i8 = $context->getTypeFromString('int8');
$typeKind = $context->builder->and($typeByte, $i8->constInt(0x7f, false));
$nullBlock = BasicBlockHelper::append($context, 'strlen_null_typeerror');
$nullErrBlock = BasicBlockHelper::append($context, 'strlen_null_typeerror');
$okBlock = BasicBlockHelper::append($context, 'strlen_value_ok');
$context->builder->branchIf(
$context->builder->icmp(
Builder::INT_EQ,
$typeKind,
$i8->constInt(VmVariable::TYPE_NULL, false)
),
$nullBlock,
$okBlock
$isNull = $context->builder->icmp(
Builder::INT_EQ,
$typeKind,
$i8->constInt(VmVariable::TYPE_NULL, false)
);
$context->builder->positionAtEnd($nullBlock);
self::emitTypeErrorAndAbort($context, 'null');
$context->builder->positionAtEnd($okBlock);
if ($context->callerStrictTypes) {
$context->builder->branchIf($isNull, $nullErrBlock, $okBlock);
$context->builder->positionAtEnd($nullErrBlock);
self::emitTypeErrorAndAbort($context, 'null');
$context->builder->positionAtEnd($okBlock);
} else {
$coerceFromNullBlock = BasicBlockHelper::append($context, 'strlen_value_null_len');
$context->builder->branchIf($isNull, $coerceFromNullBlock, $okBlock);
$context->builder->positionAtEnd($coerceFromNullBlock);

return $context->getTypeFromString('int64')->constInt(0, false);
}
$arrayTy = $i8->constInt(JITVariable::TYPE_HASHTABLE & 0x7f, false);
$objectTy = $i8->constInt(VmVariable::TYPE_OBJECT, false);
$enumCaseTy = $i8->constInt(VmVariable::TYPE_ENUM_CASE, false);
Expand Down
7 changes: 6 additions & 1 deletion ext/types/strlen.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPCompiler\JIT\Variable;

use PHPCompiler\VM\Variable as VmVariable;
use PHPCompiler\VM\InternalStrictArg;
use PHPLLVM\Value;


Expand All @@ -30,7 +31,11 @@ public function execute(Frame $frame): void {
throw new \LogicException("Expecting exactly a single argument to strlen()");
}
$var = $frame->calledArgs[0]->resolveIndirect();
$string = VmString::requireStringBuiltinArg($var, 'strlen', 0, 'string');
if (null !== $frame->parent && InternalStrictArg::isCallerStrict($frame)) {
$string = VmString::requireStringBuiltinArg($var, 'strlen', 0, 'string');
} else {
$string = VmString::coerceStringBuiltinArgNoObject($var, 'strlen', 0, 'string');
}
if (!is_null($frame->returnVar)) {
$frame->returnVar->int(VmString::byteLength($string));
}
Expand Down
7 changes: 6 additions & 1 deletion ext/types/strlen.pre
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable;

use PHPCompiler\VM\Variable as VmVariable;
use PHPCompiler\VM\InternalStrictArg;
use PHPLLVM\Value;


Expand All @@ -27,7 +28,11 @@ class strlen extends Internal {
throw new \LogicException("Expecting exactly a single argument to strlen()");
}
$var = $frame->calledArgs[0]->resolveIndirect();
$string = VmString::requireStringBuiltinArg($var, 'strlen', 0, 'string');
if (null !== $frame->parent && InternalStrictArg::isCallerStrict($frame)) {
$string = VmString::requireStringBuiltinArg($var, 'strlen', 0, 'string');
} else {
$string = VmString::coerceStringBuiltinArgNoObject($var, 'strlen', 0, 'string');
}
if (!is_null($frame->returnVar)) {
$frame->returnVar->int(VmString::byteLength($string));
}
Expand Down
10 changes: 9 additions & 1 deletion lib/VM/InternalStrictArg.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ private static function callerStrict(Frame $frame): bool

public static function isCallerStrict(Frame $frame): bool
{
return null !== $frame->parent && $frame->parent->block->strictTypes;
$walker = $frame->parent;
while (null !== $walker) {
if ($walker->block->strictTypes) {
return true;
}
$walker = $walker->parent;
}

return false;
}

private static function message(
Expand Down
14 changes: 14 additions & 0 deletions test/compliance/cases/stdlib/strlen_scalar_coerce.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
stdlib strlen() scalar operands coerce without strict_types (#11263, ext/standard/string.c)
--FILE--
<?php
foreach ([false, true, 0, 1, 1.5, null] as $v) {
try {
echo gettype($v), '=', strlen($v), ' ';
} catch (Throwable $e) {
echo gettype($v), '=', get_class($e), ' ';
}
}
echo "\n";
--EXPECT--
boolean=0 boolean=1 integer=1 integer=1 double=3 NULL=0
10 changes: 10 additions & 0 deletions test/repro/maintainer_gap_strlen_scalar_coerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

foreach ([false, true, 0, 1, 1.5, null] as $v) {
try {
echo gettype($v), '=', strlen($v), ' ';
} catch (Throwable $e) {
echo gettype($v), '=', get_class($e), ' ';
}
}
echo "\n";