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
12 changes: 0 additions & 12 deletions ext/standard/VmScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ public static function requireCaller(Frame $frame): Frame
return $frame->parent;
}

/**
* One-arg parse_str() may only populate the global symbol table ({main}), not function locals (#4034).
*
* php-src: ext/standard/basic_functions.c — php_parse_str without result array
*/
public static function requireMainScriptForParseStrOneArg(Frame $caller): void
{
if (null === $caller->block || !$caller->block->isMainScript()) {
throw new \ArgumentCountError('parse_str() expects exactly 2 arguments, 1 given');
}
}

public static function slotForName(Frame $caller, string $name): ?int
{
return $caller->block->slotIndexForVariableName($name);
Expand Down
43 changes: 13 additions & 30 deletions ext/standard/parse_str.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\Block;
use PHPCompiler\JIT\Builtin\StringParseStr;
use PHPCompiler\JIT\Builtin\TypeErrorRaise;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\JitStringArg;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\Variable;
use PHPLLVM\Value;
use ArgumentCountError;

/**
* parse_str() — query string parser (VM: ParseStrEngine; JIT compile-time: JitParseStrMaterializer; runtime: StringParseStrJit).
Expand All @@ -28,18 +28,13 @@ public function __construct()
public function execute(Frame $frame): void
{
$argc = \count($frame->calledArgs);
if ($argc < 1 || $argc > 2) {
throw new \LogicException('parse_str() requires one or two arguments in this compiler build');
if (2 !== $argc) {
throw new ArgumentCountError(\sprintf(
'parse_str() expects exactly 2 arguments, %d given',
$argc
));
}
$encodedStr = VmString::coerceStringBuiltinArg($frame->calledArgs[0], 'parse_str', 0, 'string');
if (1 === $argc) {
$caller = VmScope::requireCaller($frame);
VmScope::requireMainScriptForParseStrOneArg($caller);
$params = ParseStrEngine::parse($encodedStr);
VmParseStr::importIntoCaller($caller, $params);

return;
}

$resultArg = $frame->calledArgs[1];
$resolved = $resultArg->resolveIndirect();
Expand Down Expand Up @@ -68,25 +63,13 @@ public function execute(Frame $frame): void

public function call(Context $context, JITVariable ...$args): Value
{
if (\count($args) < 1 || \count($args) > 2) {
throw new \LogicException('parse_str() requires one or two arguments in this compiler build');
}
if (1 === \count($args)) {
$block = $context->jitCurrentBlock ?? $context->jitEnclosingBlock;
if ($block instanceof Block && !$block->isMainScript()) {
TypeErrorRaise::registerDeclarations($context);
TypeErrorRaise::ensureLinked($context);
TypeErrorRaise::emitArgumentCountError(
$context,
'parse_str() expects exactly 2 arguments, 1 given'
);

return $context->getTypeFromString('int32')->constInt(0, false);
}
if (null === JitStringArg::compileTimeLiteral($args[0])) {
StringParseStr::ensureLinked($context);
}
JitParseStr::parseIntoScope($context, $args[0]);
if (2 !== \count($args)) {
TypeErrorRaise::registerDeclarations($context);
TypeErrorRaise::ensureLinked($context);
TypeErrorRaise::emitArgumentCountError(
$context,
\sprintf('parse_str() expects exactly 2 arguments, %d given', \count($args))
);

return $context->getTypeFromString('int32')->constInt(0, false);
}
Expand Down
4 changes: 0 additions & 4 deletions test/compliance/cases/stdlib/parse_str_function_scope.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ function t(): void {
}
}
t();

parse_str('route=home&page=3');
echo (isset($route) ? 'y' : 'n'), ':', $route ?? '', ':', (isset($page) ? 'y' : 'n'), ':', $page ?? '', "\n";
--EXPECT--
parse_str() expects exactly 2 arguments, 1 given
y:home:y:3
21 changes: 7 additions & 14 deletions test/compliance/cases/stdlib/parse_str_local_scope.phpt
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
--TEST--
stdlib parse_str() without $result populates {main} locals only (#3708, #4034)
stdlib parse_str() zero-arg throws ArgumentCountError (#4050)
--FILE--
<?php
function t(): void {
try {
parse_str('a=1&b=2');
echo "no throw\n";
} catch (ArgumentCountError $e) {
echo $e->getMessage(), "\n";
}
try {
parse_str();
echo "no throw\n";
} catch (ArgumentCountError $e) {
echo $e->getMessage(), "\n";
}
t();

parse_str('route=home&page=3');
echo (isset($route) ? 'y' : 'n'), ':', $route ?? '', ':', (isset($page) ? 'y' : 'n'), ':', $page ?? '', "\n";
--EXPECT--
parse_str() expects exactly 2 arguments, 1 given
y:home:y:3
parse_str() expects exactly 2 arguments, 0 given
12 changes: 12 additions & 0 deletions test/compliance/cases/stdlib/parse_str_one_arg_script.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
stdlib parse_str() one-arg at {main} throws ArgumentCountError (#4050)
--FILE--
<?php
try {
parse_str('route=home&page=3');
echo "no throw\n";
} catch (ArgumentCountError $e) {
echo $e->getMessage(), "\n";
}
--EXPECT--
parse_str() expects exactly 2 arguments, 1 given
11 changes: 0 additions & 11 deletions test/fixtures/aot/cases/parse_str_function_scope.phpt

This file was deleted.

7 changes: 4 additions & 3 deletions test/fixtures/aot/cases/parse_str_local_scope.phpt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
--TEST--
AOT: parse_str() without $result binds {main} locals (issue #3708, #4034)
AOT: parse_str() two-arg populates result array (#4050)
--FILE--
<?php
parse_str('id=42&name=Ada');
echo $id, ':', $name, "\n";
$params = [];
parse_str('id=42&name=Ada', $params);
echo $params['id'], ':', $params['name'], "\n";
--EXPECT--
42:Ada
2 changes: 0 additions & 2 deletions test/repro-maintainer/parse_str_function_scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ function t(): void {
}
}
t();
parse_str('route=home&page=3');
echo (isset($route) ? 'y' : 'n'), ':', $route ?? '', ':', (isset($page) ? 'y' : 'n'), ':', $page ?? '', "\n";