From 5b7c009c8f8fc55c2aa3fc566a21369d4ddbfc0f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 16 Nov 2026 18:07:09 +0100 Subject: [PATCH 1/2] Fix ReflectionMethod::getClosure() on abstract methods getClosure() returns a closure over the abstract method itself. Calling it does nothing for a user method, and crashes for the method of an internal class or interface, which has no handler: (new ReflectionMethod('Countable', 'count'))->getClosure($obj)(); Throw a ReflectionException instead, like invoke() does. --- NEWS | 3 ++ ext/reflection/php_reflection.c | 7 ++++ .../ReflectionMethod_getClosure_abstract.phpt | 37 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 ext/reflection/tests/ReflectionMethod_getClosure_abstract.phpt diff --git a/NEWS b/NEWS index a6555ac871c9..d6525d0a743a 100644 --- a/NEWS +++ b/NEWS @@ -369,6 +369,9 @@ PHP NEWS - Reflection: . Fix trampoline leak when object construction fails for lazy object. (ndossche) + . Fixed ReflectionMethod::getClosure() returning a closure for abstract + methods, which crashed when called for the ones of internal classes. + (Nicolas Grekas) - SAPI: . Fixed infinite loop in sapi_header_op() for headers of 4 GiB or more. diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index c0a8b11e0cf7..1e1387177b37 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -3503,6 +3503,13 @@ ZEND_METHOD(ReflectionMethod, getClosure) GET_REFLECTION_OBJECT_PTR(mptr); + if (mptr->common.fn_flags & ZEND_ACC_ABSTRACT) { + zend_throw_exception_ex(reflection_exception_ptr, 0, + "Trying to create a closure from abstract method %s::%s()", + ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name)); + RETURN_THROWS(); + } + if (mptr->common.fn_flags & ZEND_ACC_STATIC) { zend_create_fake_closure(return_value, mptr, mptr->common.scope, mptr->common.scope, NULL); } else { diff --git a/ext/reflection/tests/ReflectionMethod_getClosure_abstract.phpt b/ext/reflection/tests/ReflectionMethod_getClosure_abstract.phpt new file mode 100644 index 000000000000..ac8a6dcd35bb --- /dev/null +++ b/ext/reflection/tests/ReflectionMethod_getClosure_abstract.phpt @@ -0,0 +1,37 @@ +--TEST-- +ReflectionMethod::getClosure() on abstract methods +--FILE-- +getClosure($object); + } catch (ReflectionException $e) { + echo $e->getMessage(), "\n"; + } +} + +?> +--EXPECT-- +Trying to create a closure from abstract method A::instance() +Trying to create a closure from abstract method A::make() +Trying to create a closure from abstract method Countable::count() +Trying to create a closure from abstract method UnitEnum::cases() From 649def2a58b7c5059841a4aac1905ff500fe148b Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 16 Nov 2026 19:30:30 +0100 Subject: [PATCH 2/2] Assert on the class of the exception in the test --- .../tests/ReflectionMethod_getClosure_abstract.phpt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/reflection/tests/ReflectionMethod_getClosure_abstract.phpt b/ext/reflection/tests/ReflectionMethod_getClosure_abstract.phpt index ac8a6dcd35bb..4f2ef9a8a715 100644 --- a/ext/reflection/tests/ReflectionMethod_getClosure_abstract.phpt +++ b/ext/reflection/tests/ReflectionMethod_getClosure_abstract.phpt @@ -24,14 +24,14 @@ $methods = [ foreach ($methods as [$method, $object]) { try { $method->getClosure($object); - } catch (ReflectionException $e) { - echo $e->getMessage(), "\n"; + } catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), "\n"; } } ?> --EXPECT-- -Trying to create a closure from abstract method A::instance() -Trying to create a closure from abstract method A::make() -Trying to create a closure from abstract method Countable::count() -Trying to create a closure from abstract method UnitEnum::cases() +ReflectionException: Trying to create a closure from abstract method A::instance() +ReflectionException: Trying to create a closure from abstract method A::make() +ReflectionException: Trying to create a closure from abstract method Countable::count() +ReflectionException: Trying to create a closure from abstract method UnitEnum::cases()