This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Diagnose flexible array member initialized with string (PR 37481)


This patch fixes PR 37481, the failure to diagnose initializing a
flexible array member with a string literal.

Bootstrapped with no regressions on i686-pc-linux-gnu.  Applied to
mainline.

2009-04-19  Joseph Myers  <joseph@codesourcery.com>

	PR c/37481
	* c-typeck.c (digest_init): Check for initializing an array with a
	string literal.

testsuite:
2009-04-19  Joseph Myers  <joseph@codesourcery.com>

	PR c/37481
	* gcc.dg/c99-flex-array-7.c: New test.

Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 146336)
+++ gcc/c-typeck.c	(working copy)
@@ -5253,6 +5253,10 @@ digest_init (tree type, tree init, bool 
 	  expr.original_type = NULL;
 	  maybe_warn_string_init (type, expr);
 
+	  if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
+	    pedwarn_init (input_location, OPT_pedantic,
+			  "initialization of a flexible array member");
+
 	  if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
 			 TYPE_MAIN_VARIANT (type)))
 	    return inside_init;
Index: gcc/testsuite/gcc.dg/c99-flex-array-7.c
===================================================================
--- gcc/testsuite/gcc.dg/c99-flex-array-7.c	(revision 0)
+++ gcc/testsuite/gcc.dg/c99-flex-array-7.c	(revision 0)
@@ -0,0 +1,17 @@
+/* Initialization of a flexible array member with a string constant
+   must be diagnosed.  PR 37481.  */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+struct s { int a; char b[]; };
+
+struct s a = { 0, "" }; /* { dg-error "initialization of a flexible array member" } */
+/* { dg-error "near init" "near init" { target *-*-* } 8 } */
+struct s b = { 0, { 0 } }; /* { dg-error "initialization of a flexible array member" } */
+/* { dg-error "near init" "near init" { target *-*-* } 10 } */
+struct s c = { 0, { } }; /* { dg-error "ISO C forbids empty initializer braces" } */
+struct s d = { .b = "" }; /* { dg-error "initialization of a flexible array member" } */
+/* { dg-error "near init" "near init" { target *-*-* } 13 } */
+struct s e = { .b = { 0 } }; /* { dg-error "initialization of a flexible array member" } */
+/* { dg-error "near init" "near init" { target *-*-* } 15 } */
+struct s f = { .b = { } }; /* { dg-error "ISO C forbids empty initializer braces" } */

-- 
Joseph S. Myers
joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]