From e3e80f60cc4c490c49dfd5565a0c6f8e81f240ef Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Mon, 2 Sep 2024 23:02:40 -0700 Subject: [PATCH] Fix GH-15718: ReflectionProperty::get{Hook,Hooks}(): handle dynamic properties For dynamic properties, instead of crashing with a segmentation fault, just say that there are no hooks. Also includes a test to prevent regression. --- ext/reflection/php_reflection.c | 12 +++-- .../tests/property_hooks/gh15718.phpt | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 ext/reflection/tests/property_hooks/gh15718.phpt diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 531db004b3ba6..289c9392124e9 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -6505,7 +6505,8 @@ ZEND_METHOD(ReflectionProperty, getHooks) GET_REFLECTION_OBJECT_PTR(ref); - if (!ref->prop->hooks) { + // ref->prop can be missing for dynamic properties + if (!ref->prop || !ref->prop->hooks) { RETURN_EMPTY_ARRAY(); } @@ -6536,11 +6537,16 @@ ZEND_METHOD(ReflectionProperty, getHook) GET_REFLECTION_OBJECT_PTR(ref); + // ref->prop can be missing for dynamic properties + if (!ref->prop || !ref->prop->hooks) { + RETURN_NULL(); + } + zend_function *hook; if (zend_string_equals_literal(Z_STR_P(zend_enum_fetch_case_name(type)), "Get")) { - hook = ref->prop->hooks ? ref->prop->hooks[ZEND_PROPERTY_HOOK_GET] : NULL; + hook = ref->prop->hooks[ZEND_PROPERTY_HOOK_GET]; } else { - hook = ref->prop->hooks ? ref->prop->hooks[ZEND_PROPERTY_HOOK_SET] : NULL; + hook = ref->prop->hooks[ZEND_PROPERTY_HOOK_SET]; } if (!hook) { diff --git a/ext/reflection/tests/property_hooks/gh15718.phpt b/ext/reflection/tests/property_hooks/gh15718.phpt new file mode 100644 index 0000000000000..1d76fdbeb6da6 --- /dev/null +++ b/ext/reflection/tests/property_hooks/gh15718.phpt @@ -0,0 +1,48 @@ +--TEST-- +ReflectionProperty::get{Hook,Hooks}() crashes on dynamic properties +--FILE-- +prop = 'foo'; + $prop = new ReflectionProperty($obj, 'prop'); + var_dump( $prop->getHooks() ); + var_dump( $prop->getHook( PropertyHookType::Get ) ); + var_dump( $prop->getHook( PropertyHookType::Set ) ); + echo PHP_EOL; +} + +?> +--EXPECTF-- +MyDynamicClass: +array(0) { +} +NULL +NULL + +stdClass: +array(0) { +} +NULL +NULL + +NonDynamicClass: + +Deprecated: Creation of dynamic property NonDynamicClass::$prop is deprecated in %sgh15718.php on line %d +array(0) { +} +NULL +NULL