Skip to content

Commit 3e99869

Browse files
committed
zend_ast: Mark zend_ast_list_add() as ZEND_ATTRIBUTE_NODISCARD
This actually caught a bug in the implementation of attributes on regular constants: A list of 4 constants with an attribute did not correctly result in an error, since the reallocated list wasn't stored anywhere.
1 parent df5a413 commit 3e99869

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

Zend/tests/attributes/constants/multiple_constants_error.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Error trying to add attributes to multiple constants at once
55

66
#[\Foo]
77
const First = 1,
8-
Second = 2;
8+
Second = 2,
9+
Third = 3,
10+
Fourth = 4;
911

1012
?>
1113
--EXPECTF--

Zend/zend_ast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static inline bool is_power_of_two(uint32_t n) {
498498
return ((n != 0) && (n == (n & (~n + 1))));
499499
}
500500

501-
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op) {
501+
ZEND_ATTRIBUTE_NODISCARD ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op) {
502502
zend_ast_list *list = zend_ast_get_list(ast);
503503
if (list->children >= 4 && is_power_of_two(list->children)) {
504504
list = zend_ast_realloc(list,
@@ -2956,7 +2956,7 @@ zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
29562956
/* Since constants are already stored in a list, just add the attributes
29572957
* to that list instead of storing them elsewhere;
29582958
* zend_compile_const_decl() checks the kind of the list elements. */
2959-
zend_ast_list_add(ast, attr);
2959+
ast = zend_ast_list_add(ast, attr);
29602960
break;
29612961
EMPTY_SWITCH_DEFAULT_CASE()
29622962
}

Zend/zend_ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ..
320320
ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...);
321321
#endif
322322

323-
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *list, zend_ast *op);
323+
ZEND_ATTRIBUTE_NODISCARD ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *list, zend_ast *op);
324324

325325
ZEND_API zend_ast *zend_ast_create_decl(
326326
zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,

Zend/zend_compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4391,7 +4391,7 @@ static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string
43914391
ZSTR_INIT_LITERAL("description", 0));
43924392
arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
43934393
}
4394-
zend_ast_list_add((zend_ast *) args, arg);
4394+
args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
43954395
}
43964396

43974397
zend_compile_call_common(result, (zend_ast*)args, fbc, lineno);

0 commit comments

Comments
 (0)