Fix attribute target validation on fake closures

Fixes GH-8982
Closes GH-9173
This commit is contained in:
Ilija Tovilo 2022-07-28 11:19:24 +02:00
parent dd241c081c
commit 565a416e87
No known key found for this signature in database
GPG Key ID: A4F5D403F118200A
3 changed files with 55 additions and 1 deletions

2
NEWS
View File

@ -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)

View File

@ -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;

View File

@ -0,0 +1,52 @@
--TEST--
GH-8982 (Attribute target validation fails when read via ReflectionFunction)
--FILE--
<?php
#[Attribute(Attribute::TARGET_FUNCTION)]
class F
{
}
#[Attribute(Attribute::TARGET_METHOD)]
class M
{
}
class C
{
#[F]
#[M]
public function m()
{
}
}
#[F]
#[M]
function f() {}
function test(string $attributeClass, $value) {
try {
var_dump((new ReflectionFunction($value))->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) {
}