From 018485ff582f63dc9317c0292229d54e5798cccb Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 16 Nov 2026 18:04:04 +0100 Subject: [PATCH 1/2] Fix ReflectionClassConstant::getValue() when $name is a reference getValue() passes the name property to zend_update_class_constant() without dereferencing it, unlike getName() and __toString(). After `$name = &$rc->name`, the zend_reference is read as a zend_string, and the TypeError of a typed constant prints bytes of the reference instead of the name of the constant. --- NEWS | 2 ++ ext/reflection/php_reflection.c | 3 +++ ...ClassConstant_getValue_name_reference.phpt | 22 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 ext/reflection/tests/ReflectionClassConstant_getValue_name_reference.phpt diff --git a/NEWS b/NEWS index a6555ac871c9..9bdc0737be9b 100644 --- a/NEWS +++ b/NEWS @@ -369,6 +369,8 @@ PHP NEWS - Reflection: . Fix trampoline leak when object construction fails for lazy object. (ndossche) + . Fixed ReflectionClassConstant::getValue() reading a reference as the name + of the constant in the error raised for a typed constant. (Nicolas Grekas) - SAPI: . Fixed infinite loop in sapi_header_op() for headers of 4 GiB or more. diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index c0a8b11e0cf7..18ab71b63604 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -4143,6 +4143,9 @@ ZEND_METHOD(ReflectionClassConstant, getValue) RETURN_THROWS(); } + ZVAL_DEREF(name); + ZEND_ASSERT(Z_TYPE_P(name) == IS_STRING); + if (Z_TYPE(ref->value) == IS_CONSTANT_AST) { zend_result result = zend_update_class_constant(ref, Z_STR_P(name), ref->ce); if (result == FAILURE) { diff --git a/ext/reflection/tests/ReflectionClassConstant_getValue_name_reference.phpt b/ext/reflection/tests/ReflectionClassConstant_getValue_name_reference.phpt new file mode 100644 index 000000000000..56bb6cbf1713 --- /dev/null +++ b/ext/reflection/tests/ReflectionClassConstant_getValue_name_reference.phpt @@ -0,0 +1,22 @@ +--TEST-- +ReflectionClassConstant::getValue() after taking a reference to the name property +--FILE-- +name; +try { + var_dump($rc->getValue()); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +Cannot assign string to class constant Test::C of type int From 94b91b3d94c2a9beeb4845d993c7a4b58a8873d5 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 16 Nov 2026 19:30:46 +0100 Subject: [PATCH 2/2] Assert on the class of the exception in the test --- .../ReflectionClassConstant_getValue_name_reference.phpt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/reflection/tests/ReflectionClassConstant_getValue_name_reference.phpt b/ext/reflection/tests/ReflectionClassConstant_getValue_name_reference.phpt index 56bb6cbf1713..b9dc70ef75be 100644 --- a/ext/reflection/tests/ReflectionClassConstant_getValue_name_reference.phpt +++ b/ext/reflection/tests/ReflectionClassConstant_getValue_name_reference.phpt @@ -13,10 +13,10 @@ $rc = new ReflectionClassConstant(Test::class, 'C'); $name = &$rc->name; try { var_dump($rc->getValue()); -} catch (TypeError $e) { - echo $e->getMessage(), "\n"; +} catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), "\n"; } ?> --EXPECT-- -Cannot assign string to class constant Test::C of type int +TypeError: Cannot assign string to class constant Test::C of type int