Skip to content

Commit 512f5f2

Browse files
Hardening, cleanup, more tests
1 parent 56a1193 commit 512f5f2

10 files changed

+96
-10
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Attribute flags are all different, TARGET_ALL includes all targets
3+
--FILE--
4+
<?php
5+
6+
function showFlag( string $name, int $value ) {
7+
$all = Attribute::TARGET_ALL;
8+
$and = $all & $value;
9+
echo "Attribute::$name = $value ($all & $value === $and)\n";
10+
}
11+
12+
showFlag( "TARGET_CLASS", Attribute::TARGET_CLASS );
13+
showFlag( "TARGET_FUNCTION", Attribute::TARGET_FUNCTION );
14+
showFlag( "TARGET_METHOD", Attribute::TARGET_METHOD );
15+
showFlag( "TARGET_PROPERTY", Attribute::TARGET_PROPERTY );
16+
showFlag( "TARGET_CLASS_CONSTANT", Attribute::TARGET_CLASS_CONSTANT );
17+
showFlag( "TARGET_PARAMETER", Attribute::TARGET_PARAMETER );
18+
showFlag( "TARGET_CONSTANT", Attribute::TARGET_CONSTANT );
19+
showFlag( "IS_REPEATABLE", Attribute::IS_REPEATABLE );
20+
21+
$all = Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION
22+
| Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY
23+
| Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_PARAMETER
24+
| Attribute::TARGET_CONSTANT;
25+
var_dump( $all, Attribute::TARGET_ALL, $all === Attribute::TARGET_ALL );
26+
27+
?>
28+
--EXPECT--
29+
Attribute::TARGET_CLASS = 1 (127 & 1 === 1)
30+
Attribute::TARGET_FUNCTION = 2 (127 & 2 === 2)
31+
Attribute::TARGET_METHOD = 4 (127 & 4 === 4)
32+
Attribute::TARGET_PROPERTY = 8 (127 & 8 === 8)
33+
Attribute::TARGET_CLASS_CONSTANT = 16 (127 & 16 === 16)
34+
Attribute::TARGET_PARAMETER = 32 (127 & 32 === 32)
35+
Attribute::TARGET_CONSTANT = 64 (127 & 64 === 64)
36+
Attribute::IS_REPEATABLE = 128 (127 & 128 === 0)
37+
int(127)
38+
int(127)
39+
bool(true)

Zend/tests/attributes/constants/allow_named_parameters.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var_dump( $attribs[0]->getArguments() );
1515
--EXPECTF--
1616
array(1) {
1717
[0]=>
18-
object(ReflectionAttribute)#2 (1) {
18+
object(ReflectionAttribute)#%d (1) {
1919
["name"]=>
2020
string(11) "MyAttribute"
2121
}

Zend/tests/attributes/constants/constant_listed_as_target-userland.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $attribs[0]->newInstance();
1818
--EXPECTF--
1919
array(1) {
2020
[0]=>
21-
object(ReflectionAttribute)#2 (1) {
21+
object(ReflectionAttribute)#%d (1) {
2222
["name"]=>
2323
string(19) "MyConstantAttribute"
2424
}

Zend/tests/attributes/constants/constant_redefined_change.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
If a constant is redefined, attributes remain unchanged (attributes change)
2+
If a constant is redefined, attributes remain unchanged (different attributes)
33
--FILE--
44
<?php
55

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Attributes with TARGET_ALL (from the default) can target constants
3+
--FILE--
4+
<?php
5+
6+
#[Attribute]
7+
class MyAttribute {}
8+
9+
#[MyAttribute]
10+
const EXAMPLE = 'Foo';
11+
12+
$ref = new ReflectionConstant('EXAMPLE');
13+
$attribs = $ref->getAttributes();
14+
var_dump( $attribs );
15+
$attribs[0]->newInstance();
16+
17+
?>
18+
--EXPECTF--
19+
array(1) {
20+
[0]=>
21+
object(ReflectionAttribute)#%d (1) {
22+
["name"]=>
23+
string(11) "MyAttribute"
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Attributes with TARGET_ALL (from an explicit parameter) can target constants
3+
--FILE--
4+
<?php
5+
6+
#[Attribute(Attribute::TARGET_ALL)]
7+
class MyAttribute {}
8+
9+
#[MyAttribute]
10+
const EXAMPLE = 'Foo';
11+
12+
$ref = new ReflectionConstant('EXAMPLE');
13+
$attribs = $ref->getAttributes();
14+
var_dump( $attribs );
15+
$attribs[0]->newInstance();
16+
17+
?>
18+
--EXPECTF--
19+
array(1) {
20+
[0]=>
21+
object(ReflectionAttribute)#%d (1) {
22+
["name"]=>
23+
string(11) "MyAttribute"
24+
}
25+
}

Zend/zend_attributes.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,14 @@ ZEND_API zend_result zend_get_attribute_value(zval *ret, zend_attribute *attr, u
255255
return SUCCESS;
256256
}
257257
zend_ast *arg_ast = Z_ASTVAL_P(ret);
258-
zend_result result;
259258
if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
260259
attr->args[i].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
261260
zend_ast *value = arg_ast->child[1];
262261
zend_ast_ref *ast_ref = zend_ast_copy(value);
263262
zval_ptr_dtor(ret);
264263
ZVAL_AST(ret, ast_ref);
265-
result = zval_update_constant_ex(ret, scope);
266-
} else {
267-
result = zval_update_constant_ex(ret, scope);
268264
}
269-
if (result != SUCCESS) {
265+
if (SUCCESS != zval_update_constant_ex(ret, scope)) {
270266
zval_ptr_dtor(ret);
271267
return FAILURE;
272268
}

Zend/zend_compile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9437,6 +9437,7 @@ static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
94379437
attributes_ast = const_ast;
94389438
continue;
94399439
}
9440+
ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
94409441
zend_ast *name_ast = const_ast->child[0];
94419442
zend_ast **value_ast_ptr = &const_ast->child[1];
94429443
zend_string *unqualified_name = zend_ast_get_str(name_ast);

Zend/zend_constants.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ void zend_constant_add_attributes(zend_constant *c, zval *attributes_ast) {
565565
zend_attribute *deprecated_attribute = zend_get_attribute_str(
566566
c->attributes,
567567
"deprecated",
568-
sizeof("deprecated")-1
568+
strlen("deprecated")
569569
);
570570

571571
if (deprecated_attribute) {

ext/reflection/tests/ReflectionConstant_getAttributes.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var_dump($reflectionConstant->getAttributes());
1212
--EXPECTF--
1313
array(1) {
1414
[0]=>
15-
object(ReflectionAttribute)#2 (1) {
15+
object(ReflectionAttribute)#%d (1) {
1616
["name"]=>
1717
string(3) "Foo"
1818
}

0 commit comments

Comments
 (0)