Skip to content

Commit db7f5fc

Browse files
committed
Validate that class attributes are PhpAttribute or PhpCompilerAttribute.
1 parent 13e53ad commit db7f5fc

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Zend/tests/attributes/rfcexample.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Attributes: RFC Example
33
--FILE--
44
<?php
55
namespace My\Attributes {
6+
use PhpAttribute;
67

78
<<PhpAttribute>>
89
class SingleArgument {

ext/reflection/php_reflection.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "zend.h"
3333
#include "zend_API.h"
3434
#include "zend_ast.h"
35+
#include "zend_attributes.h"
3536
#include "zend_exceptions.h"
3637
#include "zend_operators.h"
3738
#include "zend_constants.h"
@@ -6673,6 +6674,20 @@ ZEND_METHOD(reflection_attribute, getAsObject)
66736674
RETURN_THROWS();
66746675
}
66756676

6677+
zend_string *lower_name = zend_string_tolower_ex(ce->name, 1);
6678+
6679+
if (ce->type == ZEND_USER_CLASS && ce->info.user.attributes && zend_hash_str_exists(ce->info.user.attributes, "phpattribute", sizeof("phpattribute")-1) == 0) {
6680+
zend_string_release(lower_name);
6681+
zend_throw_error(NULL, "Attempting to use class '%s' as attribute that does not have <<PhpAttribute>>.", ZSTR_VAL(attr->name));
6682+
RETURN_THROWS();
6683+
} else if (ce->type == ZEND_INTERNAL_CLASS && zend_hash_exists(&zend_attributes_internal_validators, lower_name) == 0) {
6684+
zend_string_release(lower_name);
6685+
zend_throw_error(NULL, "Attempting to use internal class '%s' as attribute that does not have <<PhpCompilerAttribute>>.", ZSTR_VAL(attr->name));
6686+
RETURN_THROWS();
6687+
}
6688+
6689+
zend_string_release(lower_name);
6690+
66766691
count = zend_hash_num_elements(Z_ARRVAL(attr->arguments));
66776692

66786693
if (count) {

0 commit comments

Comments
 (0)