Skip to content

Commit 2cfb028

Browse files
committed
Fix class name FQN when AST dumping new and class const
Fixes GH-9447 Closes GH-9462
1 parent f8b217a commit 2cfb028

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
(Tim Starling)
88
. Fixed bug GH-9361 (Segmentation fault on script exit #9379). (cmb,
99
Christian Schneider)
10+
. Fixed bug GH-9447 (Invalid class FQN emitted by AST dump for new and class
11+
constants in constant expressions). (ilutov)
1012

1113
- DOM:
1214
. Fixed bug #79451 (DOMDocument->replaceChild on doctype causes double free).

Zend/zend_ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ static zend_result zend_ast_add_unpacked_element(zval *result, zval *expr) {
482482

483483
zend_class_entry *zend_ast_fetch_class(zend_ast *ast, zend_class_entry *scope)
484484
{
485-
return zend_fetch_class_with_scope(zend_ast_get_str(ast), ast->attr | ZEND_FETCH_CLASS_EXCEPTION, scope);
485+
return zend_fetch_class_with_scope(zend_ast_get_str(ast), (ast->attr >> ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT) | ZEND_FETCH_CLASS_EXCEPTION, scope);
486486
}
487487

488488
ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope)

Zend/zend_compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9738,8 +9738,8 @@ static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
97389738
zend_string_release_ex(class_name, 0);
97399739
if (tmp != class_name) {
97409740
zval *zv = zend_ast_get_zval(class_ast);
9741-
97429741
ZVAL_STR(zv, tmp);
9742+
class_ast->attr = ZEND_NAME_FQ;
97439743
}
97449744
}
97459745

@@ -9834,7 +9834,7 @@ static void zend_compile_const_expr_new(zend_ast **ast_ptr)
98349834
zval *class_ast_zv = zend_ast_get_zval(class_ast);
98359835
zval_ptr_dtor_nogc(class_ast_zv);
98369836
ZVAL_STR(class_ast_zv, class_name);
9837-
class_ast->attr = fetch_type;
9837+
class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
98389838
}
98399839

98409840
static void zend_compile_const_expr_args(zend_ast **ast_ptr)

Zend/zend_compile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,9 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
917917
#define ZEND_NAME_NOT_FQ 1
918918
#define ZEND_NAME_RELATIVE 2
919919

920+
/* ZEND_FETCH_ flags in class name AST of new const expression must not clash with ZEND_NAME_ flags */
921+
#define ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT 2
922+
920923
#define ZEND_TYPE_NULLABLE (1<<8)
921924

922925
#define ZEND_ARRAY_SYNTAX_LIST 1 /* list() */

ext/reflection/tests/gh9447.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
GH-9447: Invalid class FQN emitted by AST dump for new and class constants in constant expressions
3+
--FILE--
4+
<?php
5+
6+
namespace App;
7+
8+
use SomewhereElse\Qux;
9+
10+
class Foo
11+
{
12+
public function bar(
13+
$a = Bar::BAZ,
14+
$b = new Bar(),
15+
$c = new parent(),
16+
$d = new self(),
17+
$e = new namespace\Bar(),
18+
$f = new Qux(),
19+
$g = new namespace\Qux(),
20+
$i = new \Qux(),
21+
) {}
22+
}
23+
24+
$r = new \ReflectionMethod(Foo::class, 'bar');
25+
26+
foreach ($r->getParameters() as $p) {
27+
echo $p, "\n";
28+
}
29+
30+
?>
31+
--EXPECT--
32+
Parameter #0 [ <optional> $a = \App\Bar::BAZ ]
33+
Parameter #1 [ <optional> $b = new \App\Bar() ]
34+
Parameter #2 [ <optional> $c = new parent() ]
35+
Parameter #3 [ <optional> $d = new self() ]
36+
Parameter #4 [ <optional> $e = new \App\Bar() ]
37+
Parameter #5 [ <optional> $f = new \SomewhereElse\Qux() ]
38+
Parameter #6 [ <optional> $g = new \App\Qux() ]
39+
Parameter #7 [ <optional> $i = new \Qux() ]

0 commit comments

Comments
 (0)