PR 51458, a C++ bug report, mentioned in passing that the C front end accepted the invalid code
char g[] = { [7] = "abcd" }; and I noted this on my list of known C conformance issues (unfortunately no C PR was filed for this, as far as I can tell, when the C++ bug was closed). This patch fixes the code handling the case of redundant braces around a string initializer for an array not to accept it with a designator, when the string needs to be taken as an invalid initializer for an array element instead. It turns out this also fixes PR 42262, an ICE-on-invalid for a similar case. Bootstrapped with no regressions on x86_64-unknown-linux-gnu. Applied to mainline. 2013-11-29 Joseph Myers <jos...@codesourcery.com> PR c/42262 * c-typeck.c (process_init_element): Do not treat a string as initializing a whole array when used with a designator for an individual element. testsuite: 2013-11-29 Joseph Myers <jos...@codesourcery.com> PR c/42262 * gcc.dg/c99-init-5.c, gcc.dg/c99-init-6.c: New tests. Index: testsuite/gcc.dg/c99-init-5.c =================================================================== --- testsuite/gcc.dg/c99-init-5.c (revision 0) +++ testsuite/gcc.dg/c99-init-5.c (revision 0) @@ -0,0 +1,9 @@ +/* Test for designated initializers: string constants used with + designator in character array should not initialize the array as a + whole. */ +/* { dg-do compile } */ +/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */ + +char g[] = { [7] = "abcd" }; /* { dg-error "initial" } */ +char h[10][10] = { [1][1] = "abcd" }; /* { dg-error "initial" } */ +char i[10][10] = { [1] = "abcd" }; Index: testsuite/gcc.dg/c99-init-6.c =================================================================== --- testsuite/gcc.dg/c99-init-6.c (revision 0) +++ testsuite/gcc.dg/c99-init-6.c (revision 0) @@ -0,0 +1,6 @@ +/* Test for designated initializers: invalid uses of string constants + should not ICE. PR 42262. */ +/* { dg-do compile } */ +/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */ + +int a[] = { [0 ... 1] = "", [0] = "" }; /* { dg-error "initial" } */ Index: c/c-typeck.c =================================================================== --- c/c-typeck.c (revision 205526) +++ c/c-typeck.c (working copy) @@ -8504,6 +8504,7 @@ process_init_element (struct c_expr value, bool im tree orig_value = value.value; int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; bool strict_string = value.original_code == STRING_CST; + bool was_designated = designator_depth != 0; designator_depth = 0; designator_erroneous = 0; @@ -8512,6 +8513,7 @@ process_init_element (struct c_expr value, bool im char x[] = {"foo"}; */ if (string_flag && constructor_type + && !was_designated && TREE_CODE (constructor_type) == ARRAY_TYPE && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) && integer_zerop (constructor_unfilled_index)) -- Joseph S. Myers jos...@codesourcery.com