Skip to content

Commit c1b2f3d

Browse files
committed
Brace invalid variable names in AST export of interpolated strings
Also switch the AST export name tests to zend_test_compile_to_ast().
1 parent 0bfc3c9 commit c1b2f3d

6 files changed

Lines changed: 111 additions & 119 deletions

File tree

‎Zend/tests/assert/ast_export_del_in_names.phpt‎

Lines changed: 0 additions & 66 deletions
This file was deleted.

‎Zend/tests/assert/ast_export_invalid_var_names.phpt‎

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎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();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
AST export of names containing 0x7f
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
$code = <<<'PHP'
9+
<?php
10+
C::{"\x7f"};
11+
C::{"a\x7f"};
12+
C::{"\x7fa"};
13+
$o::{"\x7f"};
14+
C::{"\x7f"}();
15+
$o::{"\x7f"}();
16+
${"\x7f"};
17+
${"a\x7f"};
18+
$o->{"\x7f"};
19+
$o?->{"\x7f"};
20+
$o->{"\x7f"}();
21+
$o?->{"\x7f"}();
22+
C::${"\x7f"};
23+
"x{${"\x7f"}}y";
24+
"x{$o->{"\x7f"}}y";
25+
"{$a}\x7f";
26+
C::{"\x80"};
27+
${"\xff"};
28+
$o->{"a\x80"};
29+
"{$a}\x80";
30+
PHP;
31+
32+
$export = zend_test_compile_to_ast($code);
33+
echo addcslashes($export, "\x7f..\xff");
34+
eval("return;\n$export");
35+
36+
?>
37+
--EXPECT--
38+
C::{'\177'};
39+
C::{'a\177'};
40+
C::{'\177a'};
41+
$o::{'\177'};
42+
C::{'\177'}();
43+
$o::{'\177'}();
44+
${'\177'};
45+
${'a\177'};
46+
$o->{'\177'};
47+
$o?->{'\177'};
48+
$o->{'\177'}();
49+
$o?->{'\177'}();
50+
C::${'\177'};
51+
"x{${'\177'}}y";
52+
"x{$o->{'\177'}}y";
53+
"$a\177";
54+
C::\200;
55+
$\377;
56+
$o->a\200;
57+
"{$a}\200";
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
AST export of variable and property names that are not valid labels
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
$code = <<<'PHP'
9+
<?php
10+
${'a b'};
11+
${''};
12+
${'1a'};
13+
${"it's"};
14+
$o->{'a b'};
15+
$o?->{'a b'};
16+
$o->{'a b'}();
17+
$o?->{'a b'}();
18+
C::${'a b'};
19+
C::{'a b'}();
20+
"x{${'a b'}}y";
21+
"x{$o->{'a b'}}y";
22+
"x ${'a b'} y";
23+
"x${1}y";
24+
${'foo'};
25+
$o->{'foo'};
26+
PHP;
27+
28+
$export = zend_test_compile_to_ast($code);
29+
echo $export;
30+
eval("return;\n$export");
31+
32+
?>
33+
--EXPECT--
34+
${'a b'};
35+
${''};
36+
${'1a'};
37+
${'it\'s'};
38+
$o->{'a b'};
39+
$o?->{'a b'};
40+
$o->{'a b'}();
41+
$o?->{'a b'}();
42+
C::${'a b'};
43+
C::{'a b'}();
44+
"x{${'a b'}}y";
45+
"x{$o->{'a b'}}y";
46+
"x {${'a b'}} y";
47+
"x{${1}}y";
48+
$foo;
49+
$o->foo;

‎Zend/zend_ast.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ static ZEND_COLD void zend_ast_export_encaps_list(smart_str *str, char quote, ze
14971497
zend_ast_export_qstr(str, quote, Z_STR_P(zv));
14981498
} else if (ast->kind == ZEND_AST_VAR &&
14991499
ast->child[0]->kind == ZEND_AST_ZVAL &&
1500+
Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_STRING &&
1501+
zend_ast_valid_var_name(
1502+
Z_STRVAL_P(zend_ast_get_zval(ast->child[0])),
1503+
Z_STRLEN_P(zend_ast_get_zval(ast->child[0]))) &&
15001504
(i + 1 == list->children ||
15011505
list->child[i + 1]->kind != ZEND_AST_ZVAL ||
15021506
!zend_ast_var_needs_braces(

0 commit comments

Comments
 (0)