PR c++/77804 - Internal compiler error on incorrect initialization of new-d array
authorMartin Sebor <msebor@redhat.com>
Tue, 4 Oct 2016 17:34:00 +0000 (17:34 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Tue, 4 Oct 2016 17:34:00 +0000 (11:34 -0600)
gcc/cp/ChangeLog:

PR c++/77804
* init.c (warn_placement_new_too_small): Avoid assuming an array type
has a constant size.

gcc/testsuite/ChangeLog:

PR c++/77804
* g++.dg/warn/Wplacement-new-size-4.C: New test.

From-SVN: r240754

gcc/cp/ChangeLog
gcc/cp/init.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wplacement-new-size-4.C [new file with mode: 0644]

index 637aed4a00bdd68cc17a8fc485f126a32d3399ad..1aa8e73234c1c3f0f5f0463944389cd2bfb52861 100644 (file)
@@ -1,3 +1,9 @@
+2016-10-04  Martin Sebor  <msebor@redhat.com>
+
+       PR c++/77804
+       * init.c (warn_placement_new_too_small): Avoid assuming an array type
+       has a constant size.
+
 2016-10-04  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/77791
index 0d17370e46871f3b4a8d83ac148a2c0e6cc0cf7c..2d5877d3041bbd288dc51fd7c0073c17d3de93eb 100644 (file)
@@ -2504,7 +2504,7 @@ warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper)
              && warn_placement_new < 2)
            return;
        }
-         
+
       /* The size of the buffer can only be adjusted down but not up.  */
       gcc_checking_assert (0 <= adjust);
 
@@ -2526,8 +2526,13 @@ warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper)
       else if (nelts && CONSTANT_CLASS_P (nelts))
          bytes_need = tree_to_uhwi (nelts)
            * tree_to_uhwi (TYPE_SIZE_UNIT (type));
-      else
+      else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
        bytes_need = tree_to_uhwi (TYPE_SIZE_UNIT (type));
+      else
+       {
+         /* The type is a VLA.  */
+         return;
+       }
 
       if (bytes_avail < bytes_need)
        {
index 3b2e14a9c9c131d97f5c251a8caf0ab538bd8689..6615024165b49d6576f70ea094b75450debc1fac 100644 (file)
@@ -1,3 +1,8 @@
+2016-10-04  Martin Sebor  <msebor@redhat.com>
+
+       PR c++/77804
+       * g++.dg/warn/Wplacement-new-size-4.C: New test.
+
 2016-10-04  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/77791
diff --git a/gcc/testsuite/g++.dg/warn/Wplacement-new-size-4.C b/gcc/testsuite/g++.dg/warn/Wplacement-new-size-4.C
new file mode 100644 (file)
index 0000000..da9b1ab
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/77804 - Internal compiler error on incorrect initialization of
+// new-d array
+// { dg-do compile }
+// { dg-additional-options "-Wplacement-new -Wvla -Wno-error=vla" }
+
+void* operator new[] (__SIZE_TYPE__ n, void *p) { return p; }
+
+int main()
+{
+    char buf[256];
+    unsigned n = 10;
+    int* p = new (buf) (int[n]);  // { dg-warning "non-constant array new length must be specified without parentheses around the type-id" }
+    //  { dg-warning "ISO C\\+\\+ forbids variable length array" "vla warning" { target *-*-* } .-1 }
+}