Patchwork RFA: Fix c/57821

login
register
mail settings
Submitter Joern Rennecke
Date July 4, 2013, 7 p.m.
Message ID <20130704150020.lv606jwfogcckk0c-nzlynne@webmail.spamcop.net>
Download mbox | patch
Permalink /patch/256989/
State New
Headers show

Comments

Joern Rennecke - July 4, 2013, 7 p.m.
For this fix I've assumed that it is now the job of the various places that
do calculations with sizetypes to flag overflows where this is desired.

bootstrapped/regtested on i686-pc-linux-gnu .
2013-07-04  Joern Rennecke <joern.rennecke@embecosm.com>
gcc:
	PR c/57821
	* c/c-typeck.c (set_init_index): When folding, check for index overflow.
	* c-family/c-common.c (complete_array_type): Delay folding first index
	like other indices.  When folding, check for index overflow.
gcc/testsuite:
	PR c/57821
	* gcc.dg/large-size-array-6.c: New test.
Joseph S. Myers - July 4, 2013, 7:20 p.m.
On Thu, 4 Jul 2013, Joern Rennecke wrote:

> For this fix I've assumed that it is now the job of the various places that
> do calculations with sizetypes to flag overflows where this is desired.

Integer constants may be shared, I don't think setting TREE_OVERFLOW on 
existing constants like you do is necessarily safe.

Also note c/ and c-family/ have their own ChangeLog files.

Patch

Index: c/c-typeck.c
===================================================================
--- c/c-typeck.c	(revision 200606)
+++ c/c-typeck.c	(working copy)
@@ -7217,6 +7217,8 @@  set_init_index (tree first, tree last,
       if (last)
 	constant_expression_warning (last);
       constructor_index = convert (bitsizetype, first);
+      if (tree_int_cst_lt (constructor_index, first))
+	TREE_OVERFLOW (constructor_index) = 1;
 
       if (last)
 	{
Index: c-family/c-common.c
===================================================================
--- c-family/c-common.c	(revision 200606)
+++ c-family/c-common.c	(working copy)
@@ -9809,8 +9809,8 @@  complete_array_type (tree *ptype, tree i
 	      bool fold_p = false;
 
 	      if ((*v)[0].index)
-		maxindex = fold_convert_loc (input_location, sizetype,
-					     (*v)[0].index);
+		maxindex = (*v)[0].index, fold_p = true;
+
 	      curindex = maxindex;
 
 	      for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
@@ -9821,15 +9821,28 @@  complete_array_type (tree *ptype, tree i
 		  else
 		    {
 		      if (fold_p)
-		        curindex = fold_convert (sizetype, curindex);
+			{
+			  /* Since we treat size types now as ordinary
+			     unsigned types, we need an explicit overflow
+			     check.  */
+			  tree orig = curindex;
+		          curindex = fold_convert (sizetype, curindex);
+			  if (tree_int_cst_lt (curindex, orig))
+			    TREE_OVERFLOW (curindex) = 1;
+			}
 		      curindex = size_binop (PLUS_EXPR, curindex,
 					     size_one_node);
 		    }
 		  if (tree_int_cst_lt (maxindex, curindex))
 		    maxindex = curindex, fold_p = curfold_p;
 		}
-	       if (fold_p)
-	         maxindex = fold_convert (sizetype, maxindex);
+	      if (fold_p)
+		{
+		  tree orig = maxindex;
+	          maxindex = fold_convert (sizetype, maxindex);
+		  if (tree_int_cst_lt (maxindex, orig))
+		    TREE_OVERFLOW (maxindex) = 1;
+		}
 	    }
 	}
       else
Index: testsuite/gcc.dg/large-size-array-6.c
===================================================================
--- testsuite/gcc.dg/large-size-array-6.c	(revision 0)
+++ testsuite/gcc.dg/large-size-array-6.c	(working copy)
@@ -0,0 +1,6 @@ 
+/* PR c/57821 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+static char * name[] = {
+    [0x8000000000000000]  = "bar"
+  }; /* { dg-error "too large" } */