Skip to content

GH-19153: Validate #[\Attribute] targets #19154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.5.0alpha3

- Core:
. Fixed bug GH-19153 (#[\Attribute] validation should error on
trait/interface/enum/abstract class). (DanielEScherzer)

- Sockets:
. socket_set_option for multicast context throws a ValueError
when the socket family is not of AF_INET/AF_INET6 family. (David Carlier)
Expand Down
12 changes: 12 additions & 0 deletions Zend/tests/attributes/Attribute/Attribute_on_abstract.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
#[Attribute] on an abstract class
--FILE--
<?php

#[Attribute]
abstract class Demo {}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot apply #[Attribute] to abstract class Demo in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/attributes/Attribute/Attribute_on_enum.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
#[Attribute] on an enum
--FILE--
<?php

#[Attribute]
enum Demo {}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot apply #[Attribute] to enum Demo in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/attributes/Attribute/Attribute_on_interface.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
#[Attribute] on an interface
--FILE--
<?php

#[Attribute]
interface Demo {}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot apply #[Attribute] to interface Demo in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/attributes/Attribute/Attribute_on_trait.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
#[Attribute] on a trait
--FILE--
<?php

#[Attribute]
trait Demo {}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot apply #[Attribute] to trait Demo in %s on line %d
19 changes: 19 additions & 0 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ static void validate_allow_dynamic_properties(
scope->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES;
}

static void validate_attribute(
zend_attribute *attr, uint32_t target, zend_class_entry *scope)
{
const char *msg = NULL;
if (scope->ce_flags & ZEND_ACC_TRAIT) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You can use zend_get_object_type_case() for most of these.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that will return "Class" (or "class") if its not a trait/interface/enum, and so

  • we still need to check if it is a trait/interface/enum
  • we still need to check if it is an abstract class

it would look something like

const char *type = NULL;
if (scope->ce_flags & (ZEND_ACC_TRAIT|ZEND_ACC_INTERFACE|ZEND_ACC_ENUM)) {
	type = zend_get_object_type_case(scope, false);
} else if (scope->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
	type = "abstract class";
}
if (type != NULL) {
	zend_error_noreturn(E_ERROR, "Cannot apply #[Attribute] to %s %s", type, ZSTR_VAL(scope->name));
}

and I don't think that that is more readable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more about keeping the wording consistent, but I'm ok either way.

msg = "Cannot apply #[Attribute] to trait %s";
} else if (scope->ce_flags & ZEND_ACC_INTERFACE) {
msg = "Cannot apply #[Attribute] to interface %s";
} else if (scope->ce_flags & ZEND_ACC_ENUM) {
msg = "Cannot apply #[Attribute] to enum %s";
} else if (scope->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
msg = "Cannot apply #[Attribute] to abstract class %s";
}
if (msg != NULL) {
zend_error_noreturn(E_ERROR, msg, ZSTR_VAL(scope->name));
}
}

ZEND_METHOD(Attribute, __construct)
{
zend_long flags = ZEND_ATTRIBUTE_TARGET_ALL;
Expand Down Expand Up @@ -522,6 +540,7 @@ void zend_register_attribute_ce(void)

zend_ce_attribute = register_class_Attribute();
attr = zend_mark_internal_attribute(zend_ce_attribute);
attr->validator = validate_attribute;

zend_ce_return_type_will_change_attribute = register_class_ReturnTypeWillChange();
zend_mark_internal_attribute(zend_ce_return_type_will_change_attribute);
Expand Down
Loading