aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Austern <austern@apple.com>2003-06-20 00:33:58 +0000
committerMatt Austern <austern@gcc.gnu.org>2003-06-20 00:33:58 +0000
commit7a1d37e9102a1bbc34b4544161ddf283c667bc0b (patch)
tree179855e458e087c8af77e7b247e9693d89ff9b45
parent3a95fe8f332b8ab061d7a6d269ef0ed6ea004b07 (diff)
downloadgcc-7a1d37e9102a1bbc34b4544161ddf283c667bc0b.zip
gcc-7a1d37e9102a1bbc34b4544161ddf283c667bc0b.tar.gz
gcc-7a1d37e9102a1bbc34b4544161ddf283c667bc0b.tar.bz2
Fix for PR c++/11228, infinite loop for new int[n]().
From-SVN: r68235
-rw-r--r--gcc/cp/ChangeLog8
-rw-r--r--gcc/cp/init.c8
-rw-r--r--gcc/testsuite/ChangeLog8
-rw-r--r--gcc/testsuite/g++.dg/expr/anew1.C20
-rw-r--r--gcc/testsuite/g++.dg/expr/anew2.C20
-rw-r--r--gcc/testsuite/g++.dg/expr/anew3.C25
-rw-r--r--gcc/testsuite/g++.dg/expr/anew4.C37
7 files changed, 125 insertions, 1 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 0b620d6..ba1fb35 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,11 @@
+2003-06-19 Matt Austern <austern@apple.com>
+
+ PR c++/11228
+ * init.c (build_zero_init): Assert that number of array elements
+ is an integer constant.
+ (build_default_init) Don't use build_zero_init for arrays with
+ variable number of elements.
+
2003-06-19 Andreas Jaeger <aj@suse.de>
* cp-tree.h: Remove duplicated declarations.
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 59395ee..710f617 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -180,6 +180,9 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
-- if T is a reference type, no initialization is performed. */
+ my_friendly_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST,
+ 20030618);
+
if (type == error_mark_node)
;
else if (static_storage_p && zero_init_p (type))
@@ -232,6 +235,8 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
/* Iterate over the array elements, building initializations. */
inits = NULL_TREE;
max_index = nelts ? nelts : array_type_nelts (type);
+ my_friendly_assert (TREE_CODE (max_index) == INTEGER_CST, 20030618);
+
for (index = size_zero_node;
!tree_int_cst_lt (max_index, index);
index = size_binop (PLUS_EXPR, index, size_one_node))
@@ -291,7 +296,8 @@ build_default_init (tree type, tree nelts)
standard says we should have generated would be precisely the
same as that obtained by calling build_zero_init below, so things
work out OK. */
- if (TYPE_NEEDS_CONSTRUCTING (type))
+ if (TYPE_NEEDS_CONSTRUCTING (type)
+ || (nelts && TREE_CODE (nelts) != INTEGER_CST))
return NULL_TREE;
/* At this point, TYPE is either a POD class type, an array of POD
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4c848dd..2a1363a 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,11 @@
+2003-06-19 Matt Austern <austern@apple.com>
+
+ PR c++/11228
+ * g++.dg/anew1.C: New test.
+ * g++.dg/anew2.C: New test.
+ * g++.dg/anew3.C: New test.
+ * g++.dg/anew4.C: New test.
+
2003-06-19 Kazu Hirata <kazu@cs.umass.edu>
* gcc.c-torture/compile/simd-5.c: Don't XFAIL on H8.
diff --git a/gcc/testsuite/g++.dg/expr/anew1.C b/gcc/testsuite/g++.dg/expr/anew1.C
new file mode 100644
index 0000000..a14408a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/expr/anew1.C
@@ -0,0 +1,20 @@
+// { dg-do run }
+// PR 11228: array operator new, with zero-initialization and a variable sized array.
+// Regression test for PR
+// Author: Matt Austern <austern@apple.com>
+
+
+int* allocate(int n)
+{
+ return new int[n]();
+}
+
+int main()
+{
+ const int n = 17;
+ int* p = allocate(n);
+ for (int i = 0; i < n; ++i)
+ if (p[i] != 0)
+ return 1;
+ return 0;
+}
diff --git a/gcc/testsuite/g++.dg/expr/anew2.C b/gcc/testsuite/g++.dg/expr/anew2.C
new file mode 100644
index 0000000..b868189
--- /dev/null
+++ b/gcc/testsuite/g++.dg/expr/anew2.C
@@ -0,0 +1,20 @@
+// { dg-do run }
+// PR 11228: array operator new, with zero-initialization and a variable sized array.
+// Regression test for PR
+// Author: Matt Austern <austern@apple.com>
+
+
+double* allocate(int n)
+{
+ return new double[n]();
+}
+
+int main()
+{
+ const int n = 17;
+ double* p = allocate(n);
+ for (int i = 0; i < n; ++i)
+ if (p[i] != 0.0)
+ return 1;
+ return 0;
+}
diff --git a/gcc/testsuite/g++.dg/expr/anew3.C b/gcc/testsuite/g++.dg/expr/anew3.C
new file mode 100644
index 0000000..3223546
--- /dev/null
+++ b/gcc/testsuite/g++.dg/expr/anew3.C
@@ -0,0 +1,25 @@
+// { dg-do run }
+// PR 11228: array operator new, with zero-initialization and a variable sized array.
+// Regression test for PR
+// Author: Matt Austern <austern@apple.com>
+
+struct X
+{
+ int a;
+ double b;
+};
+
+X* allocate(int n)
+{
+ return new X[n]();
+}
+
+int main()
+{
+ const int n = 17;
+ X* p = allocate(n);
+ for (int i = 0; i < n; ++i)
+ if (p[i].a != 0 || p[i].b != 0.0)
+ return 1;
+ return 0;
+}
diff --git a/gcc/testsuite/g++.dg/expr/anew4.C b/gcc/testsuite/g++.dg/expr/anew4.C
new file mode 100644
index 0000000..8999ffb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/expr/anew4.C
@@ -0,0 +1,37 @@
+// { dg-do run }
+// PR 11228: array operator new, with zero-initialization and a variable sized array.
+// Regression test for PR
+// Author: Matt Austern <austern@apple.com>
+
+struct B
+{
+ B();
+ int n;
+};
+
+B::B()
+{
+ n = 137;
+}
+
+
+struct D : public B
+{
+ double x;
+};
+
+
+D* allocate(int n)
+{
+ return new D[n]();
+}
+
+int main()
+{
+ const int n = 17;
+ D* p = allocate(n);
+ for (int i = 0; i < n; ++i)
+ if (p[i].n != 137 || p[i].x != 0)
+ return 1;
+ return 0;
+}