Skip to content

Commit 18df69e

Browse files
DanielEScherzeriluuu1994
authored andcommitted
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. Fixes GH-15718 Closes GH-15721
1 parent a7f789e commit 18df69e

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PHP NEWS
1616
property. (ilutov)
1717
. Fixed bug GH-15693 (Unnecessary include in main.c bloats binary).
1818
(nielsdos)
19+
. Fixed bug GH-15718 (Segfault on ReflectionProperty::get{Hook,Hooks}() on
20+
dynamic properties). (DanielEScherzer)
1921

2022
- DOM:
2123
. Fixed bug GH-13988 (Storing DOMElement consume 4 times more memory in

ext/reflection/php_reflection.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6505,7 +6505,8 @@ ZEND_METHOD(ReflectionProperty, getHooks)
65056505

65066506
GET_REFLECTION_OBJECT_PTR(ref);
65076507

6508-
if (!ref->prop->hooks) {
6508+
// ref->prop can be missing for dynamic properties
6509+
if (!ref->prop || !ref->prop->hooks) {
65096510
RETURN_EMPTY_ARRAY();
65106511
}
65116512

@@ -6536,11 +6537,16 @@ ZEND_METHOD(ReflectionProperty, getHook)
65366537

65376538
GET_REFLECTION_OBJECT_PTR(ref);
65386539

6540+
// ref->prop can be missing for dynamic properties
6541+
if (!ref->prop || !ref->prop->hooks) {
6542+
RETURN_NULL();
6543+
}
6544+
65396545
zend_function *hook;
65406546
if (zend_string_equals_literal(Z_STR_P(zend_enum_fetch_case_name(type)), "Get")) {
6541-
hook = ref->prop->hooks ? ref->prop->hooks[ZEND_PROPERTY_HOOK_GET] : NULL;
6547+
hook = ref->prop->hooks[ZEND_PROPERTY_HOOK_GET];
65426548
} else {
6543-
hook = ref->prop->hooks ? ref->prop->hooks[ZEND_PROPERTY_HOOK_SET] : NULL;
6549+
hook = ref->prop->hooks[ZEND_PROPERTY_HOOK_SET];
65446550
}
65456551

65466552
if (!hook) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
ReflectionProperty::get{Hook,Hooks}() crashes on dynamic properties
3+
--FILE--
4+
<?php
5+
6+
#[\AllowDynamicProperties]
7+
class MyDynamicClass {
8+
9+
}
10+
11+
class NonDynamicClass {
12+
13+
}
14+
15+
$cases = [ MyDynamicClass::class, stdClass::class, NonDynamicClass::class ];
16+
17+
foreach ( $cases as $c ) {
18+
echo "$c:" . PHP_EOL;
19+
$obj = new $c();
20+
$obj->prop = 'foo';
21+
$prop = new ReflectionProperty($obj, 'prop');
22+
var_dump( $prop->getHooks() );
23+
var_dump( $prop->getHook( PropertyHookType::Get ) );
24+
var_dump( $prop->getHook( PropertyHookType::Set ) );
25+
echo PHP_EOL;
26+
}
27+
28+
?>
29+
--EXPECTF--
30+
MyDynamicClass:
31+
array(0) {
32+
}
33+
NULL
34+
NULL
35+
36+
stdClass:
37+
array(0) {
38+
}
39+
NULL
40+
NULL
41+
42+
NonDynamicClass:
43+
44+
Deprecated: Creation of dynamic property NonDynamicClass::$prop is deprecated in %sgh15718.php on line %d
45+
array(0) {
46+
}
47+
NULL
48+
NULL

0 commit comments

Comments
 (0)