Skip to content

Commit

Permalink
Merge branch 'PHP-8.1'
Browse files Browse the repository at this point in the history
* PHP-8.1:
  Fix attribute target validation on fake closures
  • Loading branch information
iluuu1994 committed Jul 29, 2022
2 parents fdb9e3a + 565a416 commit 2152bb2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,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;
Expand Down
52 changes: 52 additions & 0 deletions ext/reflection/tests/gh8982.phpt
Original file line number Diff line number Diff line change
@@ -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) {
}

0 comments on commit 2152bb2

Please sign in to comment.