diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index a17879698ece833d328183dbc88856e83e4018f8..9cc7e0143a93ccd87eebc62db89af23e315b5486 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -10639,17 +10639,22 @@ process_init_element (location_t loc, struct c_expr value, bool implicit, /* Handle superfluous braces around string cst as in char x[] = {"foo"}; */ - if (string_flag - && constructor_type + if (constructor_type && !was_designated && TREE_CODE (constructor_type) == ARRAY_TYPE && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) && integer_zerop (constructor_unfilled_index)) { if (constructor_stack->replacement_value.value) - error_init (loc, "excess elements in %<char%> array initializer"); - constructor_stack->replacement_value = value; - return; + { + error_init (loc, "excess elements in %<char%> array initializer"); + return; + } + else if (string_flag) + { + constructor_stack->replacement_value = value; + return; + } } if (constructor_stack->replacement_value.value != NULL_TREE) diff --git a/gcc/testsuite/gcc.dg/init-excess-3.c b/gcc/testsuite/gcc.dg/init-excess-3.c new file mode 100644 index 0000000000000000000000000000000000000000..7741261bd49a84cf9247337a32cbbc5c24a4451e --- /dev/null +++ b/gcc/testsuite/gcc.dg/init-excess-3.c @@ -0,0 +1,15 @@ +/* Test for various cases of excess initializers for char arrays, + bug 107926. */ +/* { dg-do compile } */ +/* { dg-options "" } */ + + +char s0[] = {"abc",1}; /* { dg-error "array initializer|near init" } */ +char s1[] = {"abc","a"}; /* { dg-error "array initializer|near init" } */ +char s2[] = {1,"abc"}; /* { dg-error "array initializer|near init|computable at load time" } */ +/* { dg-warning "integer from pointer without a cast" "" { target *-*-* } .-1 } */ + +char s3[5] = {"abc",1}; /* { dg-error "array initializer|near init" } */ +char s4[5] = {"abc","a"}; /* { dg-error "array initializer|near init" } */ +char s5[5] = {1,"abc"}; /* { dg-error "array initializer|near init|computable at load time" } */ +/* { dg-warning "integer from pointer without a cast" "" { target *-*-* } .-1 } */