Skip to content

Commit 3684d41

Browse files
committed
fix #72209 (ReflectionProperty::getValue() doesn't fail if object doesn't match type)
1 parent 49a7be0 commit 3684d41

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ PHP NEWS
5858
messages). (Yasuo)
5959
. Implemented FR #48532 (Allow pg_fetch_all() to index numerically). (Yasuo)
6060

61+
- Reflection:
62+
. Fix #72209 (ReflectionProperty::getValue() doesn't fail if object doesn't match type). (Joe)
63+
6164
- Session:
6265
. Improved fix for bug #68063 (Empty session IDs do still start sessions).
6366
(Yasuo)

ext/reflection/php_reflection.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5648,6 +5648,11 @@ ZEND_METHOD(reflection_property, getValue)
56485648
return;
56495649
}
56505650

5651+
if (!instanceof_function(Z_OBJCE_P(object), ref->ce)) {
5652+
_DO_THROW("Given object is not an instance of the class this property was declared in");
5653+
/* Returns from this function */
5654+
}
5655+
56515656
zend_unmangle_property_name_ex(ref->prop.name, &class_name, &prop_name, &prop_name_len);
56525657
member_p = zend_read_property(ref->ce, object, prop_name, prop_name_len, 0, &rv);
56535658
if (member_p != &rv) {

ext/reflection/tests/ReflectionProperty_getValue_error.phpt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AnotherClass {
1515
}
1616

1717
$instance = new TestClass();
18-
$instanceWithNoProperties = new AnotherClass();
18+
$invalidInstance = new AnotherClass();
1919
$propInfo = new ReflectionProperty('TestClass', 'pub2');
2020

2121
echo "Too few args:\n";
@@ -45,9 +45,9 @@ catch(Exception $exc) {
4545
echo $exc->getMessage();
4646
}
4747

48-
echo "\n\nInstance without property:\n";
48+
echo "\n\nInvalid instance:\n";
4949
$propInfo = new ReflectionProperty('TestClass', 'pub2');
50-
var_dump($propInfo->getValue($instanceWithNoProperties));
50+
var_dump($propInfo->getValue($invalidInstance));
5151

5252
?>
5353
--EXPECTF--
@@ -77,7 +77,10 @@ string(15) "static property"
7777
Protected property:
7878
Cannot access non-public member TestClass::prot
7979

80-
Instance without property:
80+
Invalid instance:
8181

82-
Notice: Undefined property: AnotherClass::$pub2 in %s on line %d
83-
NULL
82+
Fatal error: Uncaught ReflectionException: Given object is not an instance of the class this property was declared in in %s:47
83+
Stack trace:
84+
#0 %s(47): ReflectionProperty->getValue(Object(AnotherClass))
85+
#1 {main}
86+
thrown in %s on line 47

0 commit comments

Comments
 (0)