Throw in ReflectionMethod::__construct() when initialized with private parent method

Fixes GH-9470
Closes GH-9640
This commit is contained in:
Ilija Tovilo 2022-09-29 17:51:40 +02:00
parent 08e886235a
commit e186765a4d
No known key found for this signature in database
GPG key ID: A4F5D403F118200A
3 changed files with 37 additions and 1 deletions

4
NEWS
View file

@ -34,4 +34,8 @@ PHP NEWS
. Added SO_ATTACH_REUSEPORT_CBPF socket option, to give tighter control
over socket binding for a cpu core. (David Carlier)
- Reflection:
. Fix GH-9470 (ReflectionMethod constructor should not find private parent
method). (ilutov)
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

View file

@ -3281,7 +3281,9 @@ ZEND_METHOD(ReflectionMethod, __construct)
&& (mptr = zend_get_closure_invoke_method(orig_obj)) != NULL)
{
/* do nothing, mptr already set */
} else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, method_name_len)) == NULL) {
} else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, method_name_len)) == NULL
|| ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) && mptr->common.scope != ce))
{
efree(lcname);
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Method %s::%s() does not exist", ZSTR_VAL(ce->name), method_name);

View file

@ -0,0 +1,30 @@
--TEST--
GH-9470: ReflectionMethod constructor should not find private parent method
--FILE--
<?php
class A
{
public function publicMethod() {}
protected function protectedMethod() {}
private function privateMethod() {}
}
class B extends A {}
echo (string) new ReflectionMethod('B', 'publicMethod');
echo (string) new ReflectionMethod('B', 'protectedMethod');
try {
echo (string) new ReflectionMethod('B', 'privateMethod');
} catch(Throwable $e){
echo $e->getMessage(), "\n";
}
?>
--EXPECTF--
Method [ <user, inherits A> public method publicMethod ] {
@@ %s 5 - 5
}
Method [ <user, inherits A> protected method protectedMethod ] {
@@ %s 6 - 6
}
Method B::privateMethod() does not exist