c++: Fix ICE with invalid array bounds [PR93789]
authorMarek Polacek <polacek@redhat.com>
Wed, 26 Feb 2020 20:02:25 +0000 (15:02 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 27 Feb 2020 02:56:57 +0000 (21:56 -0500)
r7-2111 introduced maybe_constant_value in cp_fully_fold.
maybe_constant_value uses cxx_eval_outermost_constant_expr, which
can clear TREE_CONSTANT:
6510   else if (non_constant_p && TREE_CONSTANT (r))
[...]
6529       TREE_CONSTANT (r) = false;

In this test the array size is '(long int) "h"'.  This used to be
TREE_CONSTANT but given the change above, the flag will be cleared
when we cp_fully_fold the array size in compute_array_index_type_loc.
That means we don't emit an error in the
10391   else if (TREE_CONSTANT (size)
block in the same function, and we go on.  Then we compute ITYPE
using cp_build_binary_op and use maybe_constant_value on it and
suddenly we have something that's TREE_CONSTANT again.  And then we
crash in reshape_init_array_1 in tree_to_uhwi, because what we have
doesn't fit in an unsigned HWI.

icc accepts this code, but since we used to reject it, I see no desire
to make this work, so don't use the folded result when we've lost
the TREE_CONSTANT flag while evaluating the size.

2020-02-26  Marek Polacek  <polacek@redhat.com>

PR c++/93789 - ICE with invalid array bounds.
* decl.c (compute_array_index_type_loc): Don't use the folded
size when folding cleared TREE_CONSTANT.

* g++.dg/ext/vla22.C: New test.

gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/vla22.C [new file with mode: 0644]

index 051e415abeefd638b6c82d51b62f72f461c3161e..b2d952d822014ff7cedf2648a68227ea3161f172 100644 (file)
@@ -1,3 +1,9 @@
+2020-02-26  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/93789 - ICE with invalid array bounds.
+       * decl.c (compute_array_index_type_loc): Don't use the folded
+       size when folding cleared TREE_CONSTANT.
+
 2020-02-26  Iain Sandoe  <iain@sandoe.co.uk>
 
        * class.c (classtype_has_non_deleted_copy_ctor): New.
index 1947c4ddb7ff865c5ad5a6973ef6c71a07b880b8..e3f4b435a4905d38e66215a8343f96429af834a0 100644 (file)
@@ -10338,9 +10338,14 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size,
            pedwarn (loc, OPT_Wpedantic,
                     "size of array is not an integral constant-expression");
        }
-      /* Use the folded result for VLAs, too; it will have resolved
-        SIZEOF_EXPR.  */
-      size = folded;
+      if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
+       /* We might have lost the TREE_CONSTANT flag e.g. when we are
+          folding a conversion from a pointer to integral type.  In that
+          case issue an error below and don't treat this as a VLA.  */;
+      else
+       /* Use the folded result for VLAs, too; it will have resolved
+          SIZEOF_EXPR.  */
+       size = folded;
     }
 
   /* Normally, the array-bound will be a constant.  */
index d8a403e5139dd236f05c017d859eecdf9a0ede1b..085537d977b3a75c4526d5e9694e0e36ff53a0c4 100644 (file)
@@ -1,3 +1,8 @@
+2020-02-26  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/93789 - ICE with invalid array bounds.
+       * g++.dg/ext/vla22.C: New test.
+
 2020-02-26  David Malcolm  <dmalcolm@redhat.com>
 
        PR analyzer/93950
diff --git a/gcc/testsuite/g++.dg/ext/vla22.C b/gcc/testsuite/g++.dg/ext/vla22.C
new file mode 100644 (file)
index 0000000..2308ee7
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/93789 - ICE with invalid array bounds.
+// { dg-do compile }
+// { dg-options "" }
+
+void
+f ()
+{
+  const int tbl[(long) "h"] = { 12 }; // { dg-error "size of array .tbl. is not an integral constant-expression" }
+}