re PR c/57821 ('array is too large' error is missing when sizetype overflows)
authorJoern Rennecke <joern.rennecke@embecosm.com>
Thu, 4 Jul 2013 22:20:34 +0000 (22:20 +0000)
committerJoern Rennecke <amylaar@gcc.gnu.org>
Thu, 4 Jul 2013 22:20:34 +0000 (23:20 +0100)
gcc/c:
        PR c/57821
        * c-typeck.c (set_init_index): When folding, check for index overflow.
gcc/c-family:
        PR c/57821
        * 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.

From-SVN: r200683

gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/c/ChangeLog
gcc/c/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/large-size-array-6.c [new file with mode: 0644]

index c9a4f70e8bcc0396328d0414f9493ce30a5c68c7..ec47ffbbf5a7571e3931a821c485eb625df2e6e0 100644 (file)
@@ -1,3 +1,9 @@
+2013-07-04  Joern Rennecke <joern.rennecke@embecosm.com>
+
+       PR c/57821
+       * c-common.c (complete_array_type): Delay folding first index
+       like other indices.  When folding, check for index overflow.
+
 2013-06-27  Marc Glisse  <marc.glisse@inria.fr>
 
        PR c++/57509
index 8f7f5e52b0a9b487b1cbcbaeda5e0293a74ad06e..61300cd05c75f9b065433b2dfbb68d7771b2c536 100644 (file)
@@ -9781,6 +9781,7 @@ complete_array_type (tree *ptype, tree initial_value, bool do_default)
   tree maxindex, type, main_type, elt, unqual_elt;
   int failure = 0, quals;
   hashval_t hashcode = 0;
+  bool overflow_p = false;
 
   maxindex = size_zero_node;
   if (initial_value)
@@ -9809,8 +9810,8 @@ complete_array_type (tree *ptype, tree initial_value, bool do_default)
              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 +9822,26 @@ complete_array_type (tree *ptype, tree initial_value, bool do_default)
                  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);
+                         overflow_p |= tree_int_cst_lt (curindex, orig);
+                       }
                      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);
+                 overflow_p |= tree_int_cst_lt (maxindex, orig);
+               }
            }
        }
       else
@@ -9890,7 +9902,7 @@ complete_array_type (tree *ptype, tree initial_value, bool do_default)
 
   if (COMPLETE_TYPE_P (type)
       && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
-      && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
+      && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
     {
       error ("size of array is too large");
       /* If we proceed with the array type as it is, we'll eventually
index 71a1db12faf79154279d0d8bf952f8aff7b37691..43cc83902efd0e1c1082c0bd04b44ae38874f610 100644 (file)
@@ -1,3 +1,8 @@
+2013-07-04  Joern Rennecke <joern.rennecke@embecosm.com>
+
+       PR c/57821
+       * c-typeck.c (set_init_index): When folding, check for index overflow.
+
 2013-06-28  Balaji V. Iyer  <balaji.v.iyer@intel.com>
 
        * c-parser.c (c_parser_array_notation): Removed rejection of array
index 3a923111429821b41a0f8b18722d2c547dfdbdaf..30871db3623a08f0e836d5ae7aa468dd7f5ddba8 100644 (file)
@@ -7217,6 +7217,11 @@ 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))
+       {
+         constructor_index = copy_node (constructor_index);
+         TREE_OVERFLOW (constructor_index) = 1;
+       }
 
       if (last)
        {
index 09599ffb5de5d9e42c3d3d3b1f83d45bfd4252c5..e0320055521222fc047b27cabd2629962305a728 100644 (file)
@@ -1,3 +1,8 @@
+2013-07-04  Joern Rennecke <joern.rennecke@embecosm.com>
+
+       PR c/57821
+       * gcc.dg/large-size-array-6.c: New test.
+
 2013-07-04  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/38634
diff --git a/gcc/testsuite/gcc.dg/large-size-array-6.c b/gcc/testsuite/gcc.dg/large-size-array-6.c
new file mode 100644 (file)
index 0000000..f7da47e
--- /dev/null
@@ -0,0 +1,6 @@
+/* PR c/57821 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+static char * name[] = {
+    [0x8000000000000000]  = "bar"
+  }; /* { dg-error "too large" } */