From cad0b4b01634a7bf2d48e89df03ba7ebad8010f2 Mon Sep 17 00:00:00 2001
From: rth <rth@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Fri, 22 Mar 2002 02:03:46 +0000
Subject: [PATCH]         PR c/5597         * c-typeck.c
 (process_init_element): Flag non-static         initialization of a flexible
 array member as illegal.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@51156 138bc75d-0d04-0410-961f-82ee72b054a4
---
 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 9939009e6086..96c4fcad8435 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2002-03-21  Eric Botcazou <ebotcazou@multimania.com>
+
+	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  <amodra@bigpond.net.au>
 
 	* config/rs6000/t-linux64: New.
@@ -10,19 +16,19 @@
 
 2002-03-21  Aldy Hernandez  <aldyh@redhat.com>
 
-        * 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  <jakub@redhat.com>
 
diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c
index 1772557449c4..297b0f9e188d 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 000000000000..6ef64625b964
--- /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)" } */
+}
-- 
GitLab