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]

PR 5041, zero-length arrays initialized by []; cause gcc-3.0 to segfault


Hi,

The testcase mentioned in PR 5041:
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=5041&database=gcc

int main ()
{
int t[0] = {};
int *p=t;
}

crashes gcc 3.0 branch, but does not crash gcc on mainline.

This fix on mainline when merged to branch fixes things:

2001-09-20  DJ Delorie  <dj@redhat.com>
 
        * c-typeck.c (really_start_incremental_init): Discriminate
        between zero-length arrays and flexible arrays.
        (push_init_level): Detect zero-length arrays and handle them
        like fixed-sized arrays.
        * expr.c (store_constructor): Handle zero-length arrays and
        flexible arrays correctly.
        * doc/extend.texi: Update zero-length array notes.


Index: expr.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/expr.c,v
retrieving revision 1.351
retrieving revision 1.352
diff -u -r1.351 -r1.352
--- expr.c	2001/09/06 08:59:36	1.351
+++ expr.c	2001/09/21 00:27:58	1.352
@@ -4710,7 +4710,9 @@
       int need_to_clear;
       tree domain = TYPE_DOMAIN (type);
       tree elttype = TREE_TYPE (type);
-      int const_bounds_p = (host_integerp (TYPE_MIN_VALUE (domain), 0)
+      int const_bounds_p = (TYPE_MIN_VALUE (domain)
+			    && TYPE_MAX_VALUE (domain)
+			    && host_integerp (TYPE_MIN_VALUE (domain), 0)
 			    && host_integerp (TYPE_MAX_VALUE (domain), 0));
       HOST_WIDE_INT minelt = 0;
       HOST_WIDE_INT maxelt = 0;


Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.137
retrieving revision 1.138
diff -u -r1.137 -r1.138
--- c-typeck.c	2001/09/16 00:48:52	1.137
+++ c-typeck.c	2001/09/21 00:27:58	1.138
@@ -5190,7 +5190,8 @@
 	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
 
 	  /* Detect non-empty initializations of zero-length arrays.  */
-	  if (constructor_max_index == NULL_TREE)
+	  if (constructor_max_index == NULL_TREE
+	      && TYPE_SIZE (constructor_type))
 	    constructor_max_index = build_int_2 (-1, -1);
 
 	  constructor_index
@@ -5352,14 +5353,15 @@
 	{
 	  constructor_max_index
 	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
+
+	  /* Detect non-empty initializations of zero-length arrays.  */
+	  if (constructor_max_index == NULL_TREE
+	      && TYPE_SIZE (constructor_type))
+	    constructor_max_index = build_int_2 (-1, -1);
+
 	  constructor_index
 	    = convert (bitsizetype, 
 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
-
-	  /* ??? For GCC 3.1, remove special case initialization of
-	     zero-length array members from pop_init_level and set
-	     constructor_max_index such that we get the normal
-	     "excess elements" warning.  */
 	}
       else
 	constructor_index = bitsize_zero_node;
@@ -5438,19 +5440,9 @@
 	    constructor_type = NULL_TREE;
 	}
       else
-	{
-	  warning_init ("deprecated initialization of zero-length array");
-
-	  /* We must be initializing the last member of a top-level struct.  */
-	  if (TREE_CHAIN (constructor_fields) != NULL_TREE)
-	    {
-	      error_init ("initialization of zero-length array before end of structure");
-	      /* Discard the initializer so that we do not abort later.  */
-	      constructor_type = NULL_TREE;
-	    }
-	  else if (constructor_depth > 2)
-	    error_init ("initialization of zero-length array inside a nested context");
-	}
+	/* Zero-length arrays are no longer special, so we should no longer
+	   get here.  */
+	abort();
     }
 
   /* Warn when some struct elements are implicitly initialized to zero.  */



Index: extend.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/extend.texi,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- extend.texi	2001/08/18 21:02:43	1.23
+++ extend.texi	2001/09/21 00:27:59	1.24
@@ -1303,17 +1303,17 @@
 
 @item
 Flexible array members may only appear as the last member of a
-@code{struct} that is otherwise non-empty.  GCC currently allows
-zero-length arrays anywhere.  You may encounter problems, however,
-defining structures containing only a zero-length array.  Such usage
-is deprecated, and we recommend using zero-length arrays only in
-places in which flexible array members would be allowed.
+@code{struct} that is otherwise non-empty.
 @end itemize
 
 GCC versions before 3.0 allowed zero-length arrays to be statically
-initialized.  In addition to those cases that were useful, it also
-allowed initializations in situations that would corrupt later data.
-Non-empty initialization of zero-length arrays is now deprecated.
+initialized, as if they were flexible arrays.  In addition to those
+cases that were useful, it also allowed initializations in situations
+that would corrupt later data.  Non-empty initialization of zero-length
+arrays is now treated like any case where there are more initializer
+elements than the array holds, in that a suitable warning about "excess
+elements in array" is given, and the excess elements (all of them, in
+this case) are ignored.
 
 Instead GCC allows static initialization of flexible array members.
 This is equivalent to defining a new structure containing the original
-- 
Craig Rodrigues        
http://www.gis.net/~craigr    
rodrigc@mediaone.net          


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