Patchwork C++ PATCH for c++/49355 (ICE with new T({""}))

login
register
mail settings
Submitter Jason Merrill
Date June 30, 2011, 11:59 p.m.
Message ID <4E0D0DF2.603@redhat.com>
Download mbox | patch
Permalink /patch/102846/
State New
Headers show

Comments

Jason Merrill - June 30, 2011, 11:59 p.m.
The problem here was that stabilize_init called from build_new_1 wasn't 
doing anything to stabilize the elements of a CONSTRUCTOR, so the 
allocator argument wasn't preevaluated, and due to the cleanup for the 
data pointer its cleanup wasn't associated with the CLEANUP_POINT_EXPR, 
leading to a crash.

Tested x86_64-pc-linux-gnu, applying to trunk.

Patch

commit 5e42b54f626d5796ecb21ea2c78a7be2699a4c0a
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jun 30 17:48:10 2011 -0400

    	PR c++/49355
    	* tree.c (stabilize_init): Handle aggregate initialization.

diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index c50751f..678c7ef 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -3291,10 +3291,18 @@  stabilize_init (tree init, tree *initp)
     t = TARGET_EXPR_INITIAL (t);
   if (TREE_CODE (t) == COMPOUND_EXPR)
     t = expr_last (t);
-  if (TREE_CODE (t) == CONSTRUCTOR
-      && EMPTY_CONSTRUCTOR_P (t))
-    /* Default-initialization.  */
-    return true;
+  if (TREE_CODE (t) == CONSTRUCTOR)
+    {
+      /* Aggregate initialization: stabilize each of the field
+	 initializers.  */
+      unsigned i;
+      tree value;
+      bool good = true;
+      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, value)
+	if (!stabilize_init (value, initp))
+	  good = false;
+      return good;
+    }
 
   /* If the initializer is a COND_EXPR, we can't preevaluate
      anything.  */
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist54.C b/gcc/testsuite/g++.dg/cpp0x/initlist54.C
new file mode 100644
index 0000000..cdb2961
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist54.C
@@ -0,0 +1,13 @@ 
+// PR c++/49355
+// { dg-options -std=c++0x }
+
+#include <string>
+
+struct T {
+  std::string foobar;
+};
+
+int main()
+{
+  T* x = new T({""});
+}