From 565a416e8791505286353277de29c875432b1928 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Thu, 28 Jul 2022 11:19:24 +0200 Subject: [PATCH] Fix attribute target validation on fake closures Fixes GH-8982 Closes GH-9173 --- NEWS | 2 ++ ext/reflection/php_reflection.c | 2 +- ext/reflection/tests/gh8982.phpt | 52 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/gh8982.phpt diff --git a/NEWS b/NEWS index 1d830204148..54e8fd07f8d 100644 --- a/NEWS +++ b/NEWS @@ -72,6 +72,8 @@ PHP NEWS - Reflection: . Fixed bug GH-8943 (Fixed Reflection::getModifiersNames() with readonly modifier). (Pierrick) + . Fixed bug GH-8982 (Attribute with TARGET_METHOD is rejected on fake + closure of method). (ilutov) - Standard: . Fixed the crypt_sha256/512 api build with clang > 12. (David Carlier) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 7ce4878139d..c4291a9ab8c 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -1882,7 +1882,7 @@ ZEND_METHOD(ReflectionFunctionAbstract, getAttributes) GET_REFLECTION_OBJECT_PTR(fptr); - if (fptr->common.scope && !(fptr->common.fn_flags & ZEND_ACC_CLOSURE)) { + if (fptr->common.scope && (fptr->common.fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) != ZEND_ACC_CLOSURE) { target = ZEND_ATTRIBUTE_TARGET_METHOD; } else { target = ZEND_ATTRIBUTE_TARGET_FUNCTION; diff --git a/ext/reflection/tests/gh8982.phpt b/ext/reflection/tests/gh8982.phpt new file mode 100644 index 00000000000..0bbda8bfb91 --- /dev/null +++ b/ext/reflection/tests/gh8982.phpt @@ -0,0 +1,52 @@ +--TEST-- +GH-8982 (Attribute target validation fails when read via ReflectionFunction) +--FILE-- +getAttributes($attributeClass)[0]->newInstance()); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } +} + +$m = [new C(), 'm'](...); +$f = f(...); + +test(F::class, $f); +test(M::class, $f); +test(F::class, $m); +test(M::class, $m); + +?> +--EXPECT-- +object(F)#4 (0) { +} +Attribute "M" cannot target function (allowed targets: method) +Attribute "F" cannot target method (allowed targets: function) +object(M)#4 (0) { +}