From: Martin Uecker Date: Sun, 26 Apr 2026 16:28:16 +0000 (+0200) Subject: c: argument expressions may be evaluated too often by typeof [PR124576] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9aaedeaced055efe5041004e0643f4f3127c6ce9;p=thirdparty%2Fgcc.git c: argument expressions may be evaluated too often by typeof [PR124576] When there are multiple declarators in a declaration and the type is specified via typeof, an expression inside the argument of typeof may be evaluated multiple times. Fix this by adding a save expression. PR c/124576 gcc/c/ChangeLog: * c-decl.cc (declspecs_add_type): Add save_expr. gcc/testsuite/ChangeLog: * gcc.dg/pr124576.c: New test. --- diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 5e5193ed880..cc07e05e336 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -13136,11 +13136,12 @@ declspecs_add_type (location_t loc, struct c_declspecs *specs, } if (spec.expr) { + tree expr = save_expr (fold_convert (void_type_node, spec.expr)); if (specs->expr) - specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr), - specs->expr, spec.expr); + specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), + specs->expr, expr); else - specs->expr = spec.expr; + specs->expr = expr; specs->expr_const_operands &= spec.expr_const_operands; } } diff --git a/gcc/testsuite/gcc.dg/pr124576.c b/gcc/testsuite/gcc.dg/pr124576.c new file mode 100644 index 00000000000..b66969d60fa --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr124576.c @@ -0,0 +1,12 @@ +/* { dg-do run } */ +/* { dg-options "-std=c99" } */ + +int main() +{ + int i = 0; + int j = 0; + __typeof__((__typeof__(int[i++])*)(j++, (void*)0)) a, b, c; + if (1 != j) + __builtin_abort(); +} +