Skip to content

Commit 8f44bd9

Browse files
authored
zend_ast: Quote names of invalid variable names when exporting AST (#22294)
Fixes #22292.
1 parent 3c6b25c commit 8f44bd9

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ PHP NEWS
2424
. Fixed bug GH-22046 (The unserialize function can lead to segfault when
2525
non-Serializable internal classes are serialized back with the C format).
2626
(kocsismate)
27+
. Fixed bug GH-22292 (AST pretty printing does not correctly handle
28+
invalid variable names). (timwolla)
2729

2830
- BCMath:
2931
. Added NUL-byte validation to BCMath functions. (jorgsowa)

Zend/tests/assert/expect_015.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ assert(0 && ($a = function (): ?static {
304304
$x = "{$a}b";
305305
$x = "{$a}b";
306306
$x = " {$foo->bar} {${$foo->bar}} ";
307-
$x = " ${---} ";
307+
$x = " ${'---'} ";
308308
foo();
309309
\foo();
310310
namespace\foo();

Zend/zend_ast.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,9 +1723,14 @@ static ZEND_COLD void zend_ast_export_var(smart_str *str, zend_ast *ast, int ind
17231723
{
17241724
if (ast->kind == ZEND_AST_ZVAL) {
17251725
zval *zv = zend_ast_get_zval(ast);
1726-
if (Z_TYPE_P(zv) == IS_STRING &&
1727-
zend_ast_valid_var_name(Z_STRVAL_P(zv), Z_STRLEN_P(zv))) {
1728-
smart_str_append(str, Z_STR_P(zv));
1726+
if (Z_TYPE_P(zv) == IS_STRING) {
1727+
if (zend_ast_valid_var_name(Z_STRVAL_P(zv), Z_STRLEN_P(zv))) {
1728+
smart_str_append(str, Z_STR_P(zv));
1729+
} else {
1730+
smart_str_appends(str, "{'");
1731+
zend_ast_export_str(str, Z_STR_P(zv));
1732+
smart_str_appends(str, "'}");
1733+
}
17291734
return;
17301735
}
17311736
} else if (ast->kind == ZEND_AST_VAR) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
GH-22292: AST pretty printing does not correctly handle invalid variable names
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public function __get($name) { return $name; }
8+
}
9+
10+
try {
11+
${'---'} = 'abc';
12+
var_dump(${'---'});
13+
assert(!${'---'});
14+
} catch (Error $e) {
15+
echo $e->getMessage(), PHP_EOL;
16+
}
17+
18+
try {
19+
$f = new Foo();
20+
var_dump($f->{'---'});
21+
assert(!$f->{'---'});
22+
} catch (Error $e) {
23+
echo $e->getMessage(), PHP_EOL;
24+
}
25+
26+
?>
27+
--EXPECT--
28+
string(3) "abc"
29+
assert(!${'---'})
30+
string(3) "---"
31+
assert(!$f->{'---'})

0 commit comments

Comments
 (0)