[C PATCH] Fix initialization of flexible array members (PR c/56078)
Jakub Jelinek
jakub@redhat.com
Thu Jan 24 15:29:00 GMT 2013
Hi!
As discussed in the PR, while most of c-typeck.c handles
constructor_max_index == NULL the same as when
tree_int_cst_lt (constructor_max_index, some_index)
returns false, i.e. some_index is within bounds, there is one spot
that handles this incorrectly and one which doesn't consider NULL
constructor_max_index at all.
Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux,
ok for trunk? The 20030305-1.c testcase is now rejected with hard
errors instead of resulting just in warnings and ignored initializers,
but I believe the error is right, initialization of nested flexible array
member is what is happening in the testcase and is invalid.
2013-01-24 Jakub Jelinek <jakub@redhat.com>
PR c/56078
* c-typeck.c (set_nonincremental_init_from_string): If
constructor_max_index is NULL, treat it as if tree_int_cst_lt
returned false.
(process_init_element): Likewise.
* gcc.dg/pr56078.c: New test.
* gcc.c-torture/compile/20030305-1.c: Add dg-error lines.
--- gcc/c/c-typeck.c.jj 2013-01-23 16:23:16.000000000 +0100
+++ gcc/c/c-typeck.c 2013-01-24 11:51:10.267238683 +0100
@@ -7574,7 +7574,9 @@ set_nonincremental_init_from_string (tre
end = p + TREE_STRING_LENGTH (str);
for (purpose = bitsize_zero_node;
- p < end && !tree_int_cst_lt (constructor_max_index, purpose);
+ p < end
+ && !(constructor_max_index
+ && tree_int_cst_lt (constructor_max_index, purpose));
purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
{
if (wchar_bytes == 1)
@@ -8106,9 +8108,9 @@ process_init_element (struct c_expr valu
true, braced_init_obstack);
else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
|| TREE_CODE (constructor_type) == VECTOR_TYPE)
- && (constructor_max_index == 0
- || tree_int_cst_lt (constructor_max_index,
- constructor_index)))
+ && constructor_max_index
+ && tree_int_cst_lt (constructor_max_index,
+ constructor_index))
process_init_element (pop_init_level (1, braced_init_obstack),
true, braced_init_obstack);
else
--- gcc/testsuite/gcc.dg/pr56078.c.jj 2013-01-24 12:04:00.727858939 +0100
+++ gcc/testsuite/gcc.dg/pr56078.c 2013-01-24 12:03:10.000000000 +0100
@@ -0,0 +1,25 @@
+/* PR c/56078 */
+/* { dg-do run } */
+/* { dg-options "-std=gnu99" } */
+
+typedef __SIZE_TYPE__ size_t;
+extern int memcmp (const void *, const void *, size_t);
+extern void abort (void);
+
+struct T { int a; char b[]; };
+struct T t1 = { .a = 1, .b = "abcd", .b[0] = '2' };
+struct T t2 = { .a = 1, .b = "2bcd" };
+struct T t3 = { .a = 1, .b[2] = 'a' };
+struct T t4 = { .a = 1, .b = { '\0', '\0', 'a' } };
+struct T t5 = { .a = 1, .b = { [0] = 'a', [1] = 'b', [2] = 'c' } };
+struct T t6 = { .a = 1, .b[2] = 'c', .b[1] = 'x', .b[0] = 'a', .b[1] = 'b' };
+
+int
+main ()
+{
+ if (memcmp (t1.b, t2.b, sizeof ("abcd")) != 0
+ || memcmp (t3.b, t4.b, 3) != 0
+ || memcmp (t5.b, t6.b, 3) != 0)
+ abort ();
+ return 0;
+}
--- gcc/testsuite/gcc.c-torture/compile/20030305-1.c.jj 2008-09-05 12:54:15.000000000 +0200
+++ gcc/testsuite/gcc.c-torture/compile/20030305-1.c 2013-01-24 16:23:01.000000000 +0100
@@ -12,7 +12,7 @@ typedef struct {
} s2_t;
static s2_t s2_array[]= {
- { 1, 4 },
- { 2, 5 },
- { 3, 6 }
+ { 1, 4 }, /* { dg-error "(initialization of flexible array member|near)" } */
+ { 2, 5 }, /* { dg-error "(initialization of flexible array member|near)" } */
+ { 3, 6 } /* { dg-error "(initialization of flexible array member|near)" } */
};
Jakub
More information about the Gcc-patches
mailing list