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
38 changes: 10 additions & 28 deletions ext/spl/LimitIteratorBuiltin.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,38 +447,20 @@ public function execute(Frame $frame): void
LimitIteratorBuiltin::CLASS_LC,
'LimitIterator::seek()'
);
if (\count($frame->calledArgs) < 2) {
throw new \ArgumentCountError(
'LimitIterator::seek() expects exactly 1 argument, '
.(\count($frame->calledArgs) - 1).' given'
);
}
$offsetArg = $frame->calledArgs[1]->resolveIndirect();
if (Variable::TYPE_INTEGER !== $offsetArg->type) {
throw new \TypeError(
'LimitIterator::seek(): Argument #1 ($offset) must be of type int, '
.self::typeLabel($offsetArg).' given'
);
}
SplLimitIteratorStorage::seek($frame, $object, $offsetArg->toInt());
// php-src zim_LimitIterator_seek — ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG (#31676).
$this->requireExactUserArgCount($frame, 'LimitIterator::seek', 1);
$offset = VmMath::parseZParamLongBuiltinArgForFrame(
$frame,
1,
'LimitIterator::seek',
1,
'offset'
);
SplLimitIteratorStorage::seek($frame, $object, $offset);
if (null !== $frame->returnVar) {
$frame->returnVar->int(SplLimitIteratorStorage::position($object));
}
}

private static function typeLabel(Variable $var): string
{
return match ($var->type) {
Variable::TYPE_NULL => 'null',
Variable::TYPE_BOOLEAN => 'bool',
Variable::TYPE_INTEGER => 'int',
Variable::TYPE_FLOAT => 'float',
Variable::TYPE_STRING => 'string',
Variable::TYPE_ARRAY => 'array',
Variable::TYPE_OBJECT => 'object',
default => 'mixed',
};
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/compliance/cases/stdlib/limititerator_seek_null_soft.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
LimitIterator::seek(null) — soft-null DEP then seek 0 (#31676)
--FILE--
<?php
error_reporting(E_ALL);
set_error_handler(static function (int $no, string $msg): bool {
if (E_DEPRECATED === $no) {
echo "DEP:{$msg}\n";
return true;
}
echo "E{$no}:{$msg}\n";
return true;
});
$l = new LimitIterator(new ArrayIterator([10, 20, 30]), 0, 3);
$l->rewind();
try {
$l->seek(null);
echo 'cur=' . $l->current() . ' key=' . $l->key() . "\n";
} catch (Throwable $e) {
echo get_class($e) . ': ' . $e->getMessage() . "\n";
}
?>
--EXPECT--
DEP:LimitIterator::seek(): Passing null to parameter #1 ($offset) of type int is deprecated
cur=10 key=0
120 changes: 120 additions & 0 deletions test/unit/Issue31676LimitIteratorSeekNullTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

namespace PHPCompiler;

use PHPUnit\Framework\TestCase;

/**
* LimitIterator::seek(null) — soft-null E_DEPRECATED then seek 0 (#31676).
*
* php-src: ext/spl/spl_iterators.c — zim_LimitIterator_seek
*/
final class Issue31676LimitIteratorSeekNullTest extends TestCase
{
public function testVmSeekNullDeprecationThenOffsetZero(): void
{
$out = $this->runBin('bin/vm.php', $this->softProbeCode());
$this->assertSame($this->expectedSoftOutput(), $out);
}

public function testJitSeekNullDeprecationThenOffsetZero(): void
{
$out = $this->runBin('bin/jit.php', $this->softProbeCode());
$this->assertSame($this->expectedSoftOutput(), $out);
}

public function testVmStrictTypesSeekNullTypeError(): void
{
$out = $this->runBin('bin/vm.php', $this->strictProbeCode());
$this->assertSame(
"TypeError: LimitIterator::seek(): Argument #1 (\$offset) must be of type int, null given\n",
$out
);
}

public function testJitStrictTypesSeekNullTypeError(): void
{
$out = $this->runBin('bin/jit.php', $this->strictProbeCode());
$this->assertSame(
"TypeError: LimitIterator::seek(): Argument #1 (\$offset) must be of type int, null given\n",
$out
);
}

private function expectedSoftOutput(): string
{
return "DEP:LimitIterator::seek(): Passing null to parameter #1 (\$offset) of type int is deprecated\n"
."cur=10 key=0\n";
}

private function softProbeCode(): string
{
return <<<'PHP'
<?php
error_reporting(E_ALL);
set_error_handler(static function (int $no, string $msg): bool {
if (E_DEPRECATED === $no) {
echo "DEP:{$msg}\n";
return true;
}
echo "E{$no}:{$msg}\n";
return true;
});
$l = new LimitIterator(new ArrayIterator([10, 20, 30]), 0, 3);
$l->rewind();
try {
$l->seek(null);
echo 'cur=' . $l->current() . ' key=' . $l->key() . "\n";
} catch (Throwable $e) {
echo get_class($e) . ': ' . $e->getMessage() . "\n";
}
PHP;
}

private function strictProbeCode(): string
{
return <<<'PHP'
<?php
declare(strict_types=1);
error_reporting(E_ALL);
$l = new LimitIterator(new ArrayIterator([10, 20, 30]), 0, 3);
$l->rewind();
try {
$l->seek(null);
echo "ok\n";
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
PHP;
}

private function runBin(string $bin, string $code): string
{
$repo = dirname(__DIR__, 2);
$tmp = tempnam(sys_get_temp_dir(), 'phpc_31676_');
$this->assertNotFalse($tmp);
file_put_contents($tmp, $code);
$env = $_ENV;
LlvmToolchain::applyProcessEnv($env, $repo);
$proc = proc_open(
['php', $repo.'/'.$bin, $tmp],
[0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']],
$pipes,
$repo,
$env
);
$this->assertIsResource($proc);
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$exit = proc_close($proc);
@unlink($tmp);
$this->assertSame(0, $exit, $stdout.$stderr);

return $stdout;
}
}
Loading