From a9d0fdc7175a11c7c0da4b35f297450dee53a915 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Wed, 16 Feb 2022 12:32:17 +0100 Subject: [PATCH] Fix GH-8080: ReflectionClass::getConstants() depends on def. order When we need to evaluate constant ASTs, we always have to do that in the scope where the constant has been defined, which may be a parent of the `ReflectionClass`'s scope. --- ext/reflection/php_reflection.c | 4 ++-- ext/reflection/tests/gh8080.phpt | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 ext/reflection/tests/gh8080.phpt diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index c2c379be2823a..0210fa5ef3fd5 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -4454,7 +4454,7 @@ ZEND_METHOD(ReflectionClass, getConstants) array_init(return_value); ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, constant) { - if (UNEXPECTED(zval_update_constant_ex(&constant->value, ce) != SUCCESS)) { + if (UNEXPECTED(zval_update_constant_ex(&constant->value, constant->ce) != SUCCESS)) { RETURN_THROWS(); } @@ -4511,7 +4511,7 @@ ZEND_METHOD(ReflectionClass, getConstant) GET_REFLECTION_OBJECT_PTR(ce); ZEND_HASH_FOREACH_PTR(&ce->constants_table, c) { - if (UNEXPECTED(zval_update_constant_ex(&c->value, ce) != SUCCESS)) { + if (UNEXPECTED(zval_update_constant_ex(&c->value, c->ce) != SUCCESS)) { RETURN_THROWS(); } } ZEND_HASH_FOREACH_END(); diff --git a/ext/reflection/tests/gh8080.phpt b/ext/reflection/tests/gh8080.phpt new file mode 100644 index 0000000000000..c9c861051ab85 --- /dev/null +++ b/ext/reflection/tests/gh8080.phpt @@ -0,0 +1,31 @@ +--TEST-- +GH-8080 (ReflectionClass::getConstants() depends on def. order) +--FILE-- + 'Test', + ]; + private const TEST = 'test'; +} + +class B extends A {} + +$r = new ReflectionClass(B::class); +var_dump( + $r->getConstants(), + $r->getConstant("LIST") +); +?> +--EXPECT-- +array(1) { + ["LIST"]=> + array(1) { + ["test"]=> + string(4) "Test" + } +} +array(1) { + ["test"]=> + string(4) "Test" +}