Skip to content

Commit 6f57d75

Browse files
GH-19153: Validate #[\Attribute] targets
Do not allow #[\Attribute] on traits, interfaces, enums, or abstract classes.
1 parent 2c768f0 commit 6f57d75

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.5.0alpha3
44

5+
- Core:
6+
. Fixed bug GH-19153 (#[\Attribute] validation should error on
7+
trait/interface/enum/abstract class). (DanielEScherzer)
8+
59
- Sockets:
610
. socket_set_option for multicast context throws a ValueError
711
when the socket family is not of AF_INET/AF_INET6 family. (David Carlier)

Zend/tests/attributes/Attribute/Attribute_on_abstract.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ abstract class Demo {}
88

99
echo "Done\n";
1010
?>
11-
--EXPECT--
12-
Done
11+
--EXPECTF--
12+
Fatal error: Cannot apply #[Attribute] to abstract class Demo in %s on line %d

Zend/tests/attributes/Attribute/Attribute_on_enum.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ enum Demo {}
88

99
echo "Done\n";
1010
?>
11-
--EXPECT--
12-
Done
11+
--EXPECTF--
12+
Fatal error: Cannot apply #[Attribute] to enum Demo in %s on line %d

Zend/tests/attributes/Attribute/Attribute_on_interface.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ interface Demo {}
88

99
echo "Done\n";
1010
?>
11-
--EXPECT--
12-
Done
11+
--EXPECTF--
12+
Fatal error: Cannot apply #[Attribute] to interface Demo in %s on line %d

Zend/tests/attributes/Attribute/Attribute_on_trait.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ trait Demo {}
88

99
echo "Done\n";
1010
?>
11-
--EXPECT--
12-
Done
11+
--EXPECTF--
12+
Fatal error: Cannot apply #[Attribute] to trait Demo in %s on line %d

Zend/zend_attributes.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ static void validate_allow_dynamic_properties(
9595
scope->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES;
9696
}
9797

98+
static void validate_attribute(
99+
zend_attribute *attr, uint32_t target, zend_class_entry *scope)
100+
{
101+
const char *msg = NULL;
102+
if (scope->ce_flags & ZEND_ACC_TRAIT) {
103+
msg = "Cannot apply #[Attribute] to trait %s";
104+
} else if (scope->ce_flags & ZEND_ACC_INTERFACE) {
105+
msg = "Cannot apply #[Attribute] to interface %s";
106+
} else if (scope->ce_flags & ZEND_ACC_ENUM) {
107+
msg = "Cannot apply #[Attribute] to enum %s";
108+
} else if (scope->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
109+
msg = "Cannot apply #[Attribute] to abstract class %s";
110+
}
111+
if (msg != NULL) {
112+
zend_error_noreturn(E_ERROR, msg, ZSTR_VAL(scope->name));
113+
}
114+
}
115+
98116
ZEND_METHOD(Attribute, __construct)
99117
{
100118
zend_long flags = ZEND_ATTRIBUTE_TARGET_ALL;
@@ -522,6 +540,7 @@ void zend_register_attribute_ce(void)
522540

523541
zend_ce_attribute = register_class_Attribute();
524542
attr = zend_mark_internal_attribute(zend_ce_attribute);
543+
attr->validator = validate_attribute;
525544

526545
zend_ce_return_type_will_change_attribute = register_class_ReturnTypeWillChange();
527546
zend_mark_internal_attribute(zend_ce_return_type_will_change_attribute);

0 commit comments

Comments
 (0)