Bug #72209 [Com]: ReflectionProperty#getValue() doesn't fail if object doesn't match type

From: Date: Fri, 13 May 2016 22:56:10 +0000
Subject: Bug #72209 [Com]: ReflectionProperty#getValue() doesn't fail if object doesn't match type
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201080@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72209&edit=1 ID: 72209 Comment by: ocramius at gmail dot com Reported by: ocramius at gmail dot com Summary: ReflectionProperty#getValue() doesn't fail if object doesn't match type Status: Open Type: Bug Package: Reflection related PHP Version: 7.0.7RC1 Block user comment: N Private report: N New Comment: Indeed, that last test makes no sense: https://github.com/php/php-src/blob/8462b353d0ca8840788cdf0fcceea28bc9b0f57e/ext/reflection/tests/ReflectionProperty_getValue_error.phpt#L48-L50 Nothing useful can be done with a ReflectionProperty against an invalid instance, and therefore there are no BC breaks either. Previous Comments: ------------------------------------------------------------------------ [2016-05-13 09:35:26] krakjoe@php.net This can't be fixed without changing a test, and the test doesn't make any sense at all. The test seems to imply that what you are seeing is expected behaviour ... Here's a patch: diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 4c3f624..ae1e932 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -5382,6 +5382,11 @@ ZEND_METHOD(reflection_property, getValue) return; } + if (!instanceof_function(Z_OBJCE_P(object), ref->ce)) { + _DO_THROW("Given object is not an instance of the class this property was declared in"); + /* Returns from this function */ + } + zend_unmangle_property_name_ex(ref->prop.name, &class_name, &prop_name, &prop_name_len); member_p = zend_read_property(ref->ce, object, prop_name, prop_name_len, 0, &rv); if (member_p != &rv) { This is the stupid test (last test in ReflectionProperty_getValue_error.phpt): echo "\n\nInstance without property:\n"; $propInfo = new ReflectionProperty('TestClass', 'pub2'); var_dump($propInfo->getValue($instanceWithNoProperties)); ------------------------------------------------------------------------ [2016-05-12 15:37:49] ocramius at gmail dot com Description: ------------ ReflectionProperty#getValue($object) does not check whether the given $object matches the expected type. If $r = ReflectionProperty(Foo::class, 'bar'), then only instances of Foo should be allowed by ReflectionProperty#getValue() and ReflectionProperty#setValue(). All other values should cause an exception to be raised. As a reference, ReflectionMethod behaves correctly ( https://3v4l.org/U2u0h ), since ReflectionMethod#invoke($object) and ReflectionMethod#invokeArgs($object) reject incompatible $object instances. Test script: --------------- <?php class Foo { public $bar = __LINE__; } class Baz { public $bar = __LINE__; } var_dump((new ReflectionProperty(Foo::class, 'bar'))->getValue(new Baz())); Expected result: ---------------- Fatal error: Uncaught ReflectionException: Expected an instance of Foo, got 'Baz' instead does not exist in /.../test-script.php Actual result: -------------- (int) 10 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72209&edit=1

« previous php.bugs (#201080) next »