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.
This commit is contained in:
Martin Uecker
2026-04-26 18:28:16 +02:00
committed by Martin Uecker
parent 9c40f8de18
commit 9aaedeaced
2 changed files with 16 additions and 3 deletions

View 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;
}
}

View File

@@ -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();
}