]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: argument expressions may be evaluated too often by typeof [PR124576]
authorMartin Uecker <uecker@tugraz.at>
Sun, 26 Apr 2026 16:28:16 +0000 (18:28 +0200)
committerMartin Uecker <uecker@gcc.gnu.org>
Fri, 1 May 2026 20:20:09 +0000 (22:20 +0200)
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.

gcc/c/c-decl.cc
gcc/testsuite/gcc.dg/pr124576.c [new file with mode: 0644]

index 5e5193ed8809ab22a5a56c4d9faae8b5fa691d4c..cc07e05e33634669a0842e33b927aef241d57006 100644 (file)
@@ -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 (file)
index 0000000..b66969d
--- /dev/null
@@ -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();
+}
+