Skip to content

Commit 6ba5f7e

Browse files
TimWolladevnexen
andauthored
zend_ast: Mark zend_ast_list_add() as ZEND_ATTRIBUTE_NODISCARD (#19632)
* zend_portability: Add `ZEND_ATTRIBUTE_NODISCARD` * 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. * zend_portability: Support C++ for `ZEND_ATTRIBUTE_NODISCARD` Co-authored-by: David CARLIER <[email protected]> --------- Co-authored-by: David CARLIER <[email protected]>
1 parent 34a0bcf commit 6ba5f7e

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-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);

Zend/zend_portability.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ char *alloca();
248248
# define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y)
249249
#endif
250250

251+
#if __STDC_VERSION__ >= 202311L || (defined(__cplusplus) && __cplusplus >= 201703L)
252+
# define ZEND_ATTRIBUTE_NODISCARD [[nodiscard]]
253+
#elif __has_attribute(__warn_unused_result__)
254+
# define ZEND_ATTRIBUTE_NODISCARD __attribute__((__warn_unused_result__))
255+
#else
256+
# define ZEND_ATTRIBUTE_NODISCARD
257+
#endif
258+
251259
#if ZEND_GCC_VERSION >= 3000
252260
# define ZEND_ATTRIBUTE_CONST __attribute__((const))
253261
#else

0 commit comments

Comments
 (0)