From 033eec7bb1cd0ba948176a0df6b09e7485896097 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Tue, 8 Oct 2024 19:09:30 -0700 Subject: [PATCH 1/2] Reflection: show the type of object constants used as default properties When a property default is based on a global constant, show the type of the default. Previously, `format_default_value()` assumed that non-scalar and non-array defaults were always going to be `IS_CONSTANT_AST` pointers, and when the AST expression had been evaluated and produced an object, depending on when the `ReflectionClass` or `ReflectionProperty` instance had been created, the default was shown as one of `callable`, `__CLASS__`, or `...`. Instead, if the default value is an object (`IS_OBJECT`), show the type of that object. Add test cases for each of the `callable`, `__CLASS__`, and `...` cases to confirm that they all now properly show the type of the constant. Closes gh-15902 --- NEWS | 4 ++ ext/reflection/php_reflection.c | 16 +++++--- .../gh15902/ReflectionClass-callable.phpt | 36 ++++++++++++++++++ .../tests/gh15902/ReflectionClass-class.phpt | 37 +++++++++++++++++++ .../gh15902/ReflectionProperty-callable.phpt | 19 ++++++++++ .../gh15902/ReflectionProperty-class.phpt | 19 ++++++++++ 6 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 ext/reflection/tests/gh15902/ReflectionClass-callable.phpt create mode 100644 ext/reflection/tests/gh15902/ReflectionClass-class.phpt create mode 100644 ext/reflection/tests/gh15902/ReflectionProperty-callable.phpt create mode 100644 ext/reflection/tests/gh15902/ReflectionProperty-class.phpt diff --git a/NEWS b/NEWS index 103b778aacf4..8b01cda2e84f 100644 --- a/NEWS +++ b/NEWS @@ -57,6 +57,10 @@ PHP NEWS - PHPDBG: . Fixed bug GH-16174 (Empty string is an invalid expression for ev). (cmb) +- Reflection: + . Fixed bug GH-15902 (Core dumped in ext/reflection/php_reflection.c). + (DanielEScherzer) + - Session: . Fixed bug GH-16385 (Unexpected null returned by session_set_cookie_params). (nielsdos) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 3faf8ed78681..cbb071626af8 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -639,13 +639,19 @@ static int format_default_value(smart_str *str, zval *value) { } ZEND_HASH_FOREACH_END(); smart_str_appendc(str, ']'); } else if (Z_TYPE_P(value) == IS_OBJECT) { - /* This branch may only be reached for default properties, which don't support arbitrary objects. */ + /* This branch may only be reached for default properties; show enum names, + or the type of non-enums (GH-15902) */ zend_object *obj = Z_OBJ_P(value); zend_class_entry *class = obj->ce; - ZEND_ASSERT(class->ce_flags & ZEND_ACC_ENUM); - smart_str_append(str, class->name); - smart_str_appends(str, "::"); - smart_str_append(str, Z_STR_P(zend_enum_fetch_case_name(obj))); + if (class->ce_flags & ZEND_ACC_ENUM) { + smart_str_append(str, class->name); + smart_str_appends(str, "::"); + smart_str_append(str, Z_STR_P(zend_enum_fetch_case_name(obj))); + } else { + smart_str_appends(str, "object("); + smart_str_append(str, class->name); + smart_str_appends(str, ")"); + } } else { ZEND_ASSERT(Z_TYPE_P(value) == IS_CONSTANT_AST); zend_string *ast_str = zend_ast_export("", Z_ASTVAL_P(value), ""); diff --git a/ext/reflection/tests/gh15902/ReflectionClass-callable.phpt b/ext/reflection/tests/gh15902/ReflectionClass-callable.phpt new file mode 100644 index 000000000000..62669554b245 --- /dev/null +++ b/ext/reflection/tests/gh15902/ReflectionClass-callable.phpt @@ -0,0 +1,36 @@ +--TEST-- +ReflectionClass object default property - used to say "callable" +--FILE-- + +--EXPECTF-- +string(%d) "Class [ class C ] { + @@ %sReflectionClass-callable.php %d-%d + + - Constants [0] { + } + + - Static properties [0] { + } + + - Static methods [0] { + } + + - Properties [1] { + Property [ public stdClass $a = object(stdClass) ] + } + + - Methods [0] { + } +} +" diff --git a/ext/reflection/tests/gh15902/ReflectionClass-class.phpt b/ext/reflection/tests/gh15902/ReflectionClass-class.phpt new file mode 100644 index 000000000000..e95d50123f0f --- /dev/null +++ b/ext/reflection/tests/gh15902/ReflectionClass-class.phpt @@ -0,0 +1,37 @@ +--TEST-- +ReflectionClass object default property - used to say "__CLASS__" +--FILE-- + +--EXPECTF-- +string(%d) "Class [ class C ] { + @@ %sReflectionClass-class.php %d-%d + + - Constants [0] { + } + + - Static properties [0] { + } + + - Static methods [0] { + } + + - Properties [1] { + Property [ public stdClass $a = object(stdClass) ] + } + + - Methods [0] { + } +} +" diff --git a/ext/reflection/tests/gh15902/ReflectionProperty-callable.phpt b/ext/reflection/tests/gh15902/ReflectionProperty-callable.phpt new file mode 100644 index 000000000000..64402f1bfe60 --- /dev/null +++ b/ext/reflection/tests/gh15902/ReflectionProperty-callable.phpt @@ -0,0 +1,19 @@ +--TEST-- +ReflectionProperty object default - used to say "callable" +--FILE-- + +--EXPECTF-- +string(%d) "Property [ public stdClass $a = object(stdClass) ] +" diff --git a/ext/reflection/tests/gh15902/ReflectionProperty-class.phpt b/ext/reflection/tests/gh15902/ReflectionProperty-class.phpt new file mode 100644 index 000000000000..454403f9eeca --- /dev/null +++ b/ext/reflection/tests/gh15902/ReflectionProperty-class.phpt @@ -0,0 +1,19 @@ +--TEST-- +ReflectionProperty object default - used to say "__CLASS__" +--FILE-- + +--EXPECTF-- +string(%d) "Property [ public stdClass $a = object(stdClass) ] +" From 77b8ec195414f4b77f9696983b9503ce990e27e6 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Mon, 21 Oct 2024 15:28:53 -0700 Subject: [PATCH 2/2] property_get_default(): update class constants --- ext/reflection/php_reflection.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index cbb071626af8..88ca1cd63424 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -884,7 +884,10 @@ static zval *property_get_default(zend_property_info *prop_info) { ZVAL_DEINDIRECT(prop); return prop; } else { - return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]; + if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) { + return &EG(uninitialized_zval); + } + return &CE_DEFAULT_PROPERTIES_TABLE(ce)[OBJ_PROP_TO_NUM(prop_info->offset)]; } }