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
18 changes: 17 additions & 1 deletion ext/standard/JitStreamFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,23 @@ private static function attach(Context $context, string $abi, string $functionNa
JitLongArg::lower($context, $args[0], $functionName.'() stream'),
$context->getTypeFromString('int64')
);
$filterName = JitStringBuiltinArg::lower($context, $args[1], $functionName, 1, 'filtername');
// Z_PARAM_STR $filter_name — TypeError under declare(strict_types=1) (#31408).
$filterName = JitStringBuiltinArg::lowerStrictOrCoercible(
$context,
$args[1],
$functionName,
1,
'filter_name'
);
// Catchable TypeError seals the insert block; open a dead BB so later attach
// IR does not land after a terminator (#31408 / peer #30250 JitDl).
if ($context->callerStrictTypes
&& (JITVariable::TYPE_NULL === $args[1]->type || ($args[1]->isNullConstant ?? false))
) {
BasicBlockHelper::ensureOpenInsertBlock($context, $functionName.'_strict_null_dead');

return self::boolBox($context, $context->getTypeFromString('int1')->constInt(0, false));
}
$i64 = $context->getTypeFromString('int64');
$readWrite = $i64->constInt(VmStreamFilterChain::ALL, false);
if ($argc >= 3) {
Expand Down
8 changes: 5 additions & 3 deletions ext/standard/stream_filter_append.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\InternalStrictArg;
use PHPLLVM\Value;

/** stream_filter_append() — attach a filter to a stream (#3283, ext/standard/streams.c). */
Expand All @@ -31,10 +32,11 @@ public function execute(Frame $frame): void
'stream_filter_append',
1
);
$filterName = VmString::coerceStringBuiltinArg(
$frame->calledArgs[1],
'stream_filter_append',
// Z_PARAM_STR $filter_name — TypeError under declare(strict_types=1) (#31408).
$filterName = InternalStrictArg::resolveCoercibleStringArg(
$frame,
1,
'stream_filter_append',
'filter_name'
);
$readWrite = VmStreamFilterChain::ALL;
Expand Down
8 changes: 5 additions & 3 deletions ext/standard/stream_filter_prepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\InternalStrictArg;
use PHPLLVM\Value;

/** stream_filter_prepend() — attach a filter at the head of a stream chain (#3283). */
Expand All @@ -31,10 +32,11 @@ public function execute(Frame $frame): void
'stream_filter_prepend',
1
);
$filterName = VmString::coerceStringBuiltinArg(
$frame->calledArgs[1],
'stream_filter_prepend',
// Z_PARAM_STR $filter_name — TypeError under declare(strict_types=1) (#31408).
$filterName = InternalStrictArg::resolveCoercibleStringArg(
$frame,
1,
'stream_filter_prepend',
'filter_name'
);
$readWrite = VmStreamFilterChain::ALL;
Expand Down
17 changes: 13 additions & 4 deletions test/repro/maintainer_gap_stream_filter_append_null_name.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?php
declare(strict_types=1);
// Maintainer gap probe: stream_filter_append($stream, null) under strict_types.
// Maintainer gap probe: stream_filter_append/prepend($stream, null) under strict_types (#31408).
// Zend: TypeError Argument #2 ($filter_name) must be of type string, null given
// VM (2026-08-16): coerces null→"" then warns unable to locate filter ""
$h = fopen('php://memory', 'r+');
var_export(stream_filter_append($h, null));
echo "\n";
try {
stream_filter_append($h, null);
echo "NO_THROW_APPEND\n";
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
try {
stream_filter_prepend($h, null);
echo "NO_THROW_PREPEND\n";
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
fclose($h);
69 changes: 69 additions & 0 deletions test/unit/Issue31408StreamFilterNullNameStrictAotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace PHPCompiler;

use PHPUnit\Framework\TestCase;

/**
* AOT: stream_filter_append/prepend null $filter_name under strict_types → TypeError (#31408).
*
* php-src: ext/standard/streamsfuncs.c / basic_functions.stub.php — string $filter_name
*
* @group llvm
* @group aot
*/
final class Issue31408StreamFilterNullNameStrictAotTest extends TestCase
{
public function testAotNullFilterNameStrictTypeError(): void
{
if (!LlvmToolchain::hasLibrary(dirname(__DIR__, 2))) {
$this->markTestSkipped('LLVM 9 toolchain not available');
}
$root = dirname(__DIR__, 2);
$src = sys_get_temp_dir().'/phpc_31408_'.getmypid().'.php';
$bin = sys_get_temp_dir().'/phpc_31408_'.getmypid().'.bin';
file_put_contents($src, <<<'PHP'
<?php
declare(strict_types=1);
$h = fopen('php://memory', 'r+');
try {
stream_filter_append($h, null);
echo "NO_THROW_APPEND\n";
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
try {
stream_filter_prepend($h, null);
echo "NO_THROW_PREPEND\n";
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
fclose($h);
PHP);
$compile = escapeshellarg(PHP_BINARY).' '
.escapeshellarg($root.'/bin/compile.php')
.' -o '.escapeshellarg($bin).' '.escapeshellarg($src).' 2>&1';
exec($compile, $compileOut, $compileRc);
$this->assertSame(0, $compileRc, implode("\n", $compileOut));
$this->assertFileExists($bin);
try {
for ($i = 0; $i < 3; ++$i) {
$runOut = [];
exec(escapeshellarg($bin).' 2>&1', $runOut, $runRc);
$this->assertSame(0, $runRc, 'run '.($i + 1).': '.implode("\n", $runOut));
$joined = implode("\n", $runOut)."\n";
$this->assertSame(
"TypeError: stream_filter_append(): Argument #2 (\$filter_name) must be of type string, null given\n"
."TypeError: stream_filter_prepend(): Argument #2 (\$filter_name) must be of type string, null given\n",
$joined
);
$this->assertStringNotContainsString('unable to locate filter', $joined);
}
} finally {
@unlink($src);
@unlink($bin);
}
}
}
33 changes: 33 additions & 0 deletions test/unit/Issue31408StreamFilterNullNameStrictTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace PHPCompiler;

use PHPUnit\Framework\TestCase;

/**
* stream_filter_append/prepend null $filter_name under strict_types → TypeError (#31408).
*
* php-src: ext/standard/streamsfuncs.c / basic_functions.stub.php — string $filter_name
*/
final class Issue31408StreamFilterNullNameStrictTest extends TestCase
{
public function testVmNullFilterNameStrictTypeError(): void
{
$code = file_get_contents(__DIR__.'/../repro/maintainer_gap_stream_filter_append_null_name.php');
$this->assertNotFalse($code);
$rt = new Runtime();
$block = $rt->parseAndCompile($code, 'maintainer_gap_stream_filter_append_null_name.php');
ob_start();
$rt->run($block);
$out = ob_get_clean();
$this->assertSame(
"TypeError: stream_filter_append(): Argument #2 (\$filter_name) must be of type string, null given\n"
."TypeError: stream_filter_prepend(): Argument #2 (\$filter_name) must be of type string, null given\n",
$out
);
$this->assertStringNotContainsString('unable to locate filter', $out);
$this->assertStringNotContainsString('NO_THROW', $out);
}
}
Loading