From eba80994397693d6ab3a65a05a05ef743ad76652 Mon Sep 17 00:00:00 2001 From: Eric Botcazou Date: Thu, 21 Mar 2002 18:03:46 -0800 Subject: [PATCH] re PR c/5597 ([regression from 2.95.3] Initialization of flexible char array member segfaults) PR c/5597 * c-typeck.c (process_init_element): Flag non-static initialization of a flexible array member as illegal. From-SVN: r51156 --- gcc/ChangeLog | 22 ++++++++++++++-------- gcc/c-typeck.c | 10 ++++++++++ gcc/testsuite/gcc.dg/array-6.c | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/array-6.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 9939009e608..96c4fcad843 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2002-03-21 Eric Botcazou + + PR c/5597 + * c-typeck.c (process_init_element): Flag non-static + initialization of a flexible array member as illegal. + 2002-03-22 Alan Modra * config/rs6000/t-linux64: New. @@ -10,19 +16,19 @@ 2002-03-21 Aldy Hernandez - * langhooks.c (lhd_tree_inlining_cannot_inline_tree_fn): Check - flag_really_no_inline instead of optimize == 0. + * langhooks.c (lhd_tree_inlining_cannot_inline_tree_fn): Check + flag_really_no_inline instead of optimize == 0. - * c-objc-common.c (c_cannot_inline_tree_fn): Same. + * c-objc-common.c (c_cannot_inline_tree_fn): Same. - * cp/tree.c (cp_cannot_inline_tree_fn): Same. + * cp/tree.c (cp_cannot_inline_tree_fn): Same. - * flags.h (flag_really_no_inline): New. + * flags.h (flag_really_no_inline): New. - * c-common.c (c_common_post_options): Initialzie - flag_really_no_inline. + * c-common.c (c_common_post_options): Initialzie + flag_really_no_inline. - * toplev.c (flag_really_no_inline): New. + * toplev.c (flag_really_no_inline): New. 2002-03-21 Jakub Jelinek diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 1772557449c..297b0f9e188 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -6537,6 +6537,16 @@ process_init_element (value) fieldtype = TYPE_MAIN_VARIANT (fieldtype); fieldcode = TREE_CODE (fieldtype); + /* Error for non-static initialization of a flexible array member. */ + if (fieldcode == ARRAY_TYPE + && !require_constant_value + && TYPE_SIZE (fieldtype) == NULL_TREE + && TREE_CHAIN (constructor_fields) == NULL_TREE) + { + error_init ("non-static initialization of a flexible array member"); + break; + } + /* Accept a string constant to initialize a subarray. */ if (value != 0 && fieldcode == ARRAY_TYPE diff --git a/gcc/testsuite/gcc.dg/array-6.c b/gcc/testsuite/gcc.dg/array-6.c new file mode 100644 index 00000000000..6ef64625b96 --- /dev/null +++ b/gcc/testsuite/gcc.dg/array-6.c @@ -0,0 +1,18 @@ +/* PR c/5597 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +/* Verify that GCC forbids non-static initialization of + flexible array members. */ + +struct str { int len; char s[]; }; + +struct str a = { 2, "a" }; + +void foo() +{ + static struct str b = { 2, "b" }; + struct str c = { 2, "c" }; /* { dg-error "(non-static)|(near initialization)" } */ + struct str d = (struct str) { 2, "d" }; /* { dg-error "(non-static)|(near initialization)" } */ + struct str e = (struct str) { d.len, "e" }; /* { dg-error "(non-static)|(initialization)" } */ +} -- 2.30.2