]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c++/50618 (Virtual inheritance segfault)
authorJason Merrill <jason@redhat.com>
Thu, 13 Oct 2011 18:02:27 +0000 (14:02 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Thu, 13 Oct 2011 18:02:27 +0000 (14:02 -0400)
PR c++/50618
* init.c (expand_aggr_init_1): Don't zero-initialize virtual
bases of a base subobject.

From-SVN: r179936

gcc/cp/ChangeLog
gcc/cp/init.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/init/vbase1.C [new file with mode: 0644]

index c4542248c33123af8351759919312a005266e290..f4da489d6e950f6b3f8510d569e22be6533bd0eb 100644 (file)
@@ -1,3 +1,9 @@
+2011-10-13  Jason Merrill  <jason@redhat.com>
+
+       PR c++/50618
+       * init.c (expand_aggr_init_1): Don't zero-initialize virtual
+       bases of a base subobject.
+
 2011-10-11  Janis Johnson  <janisjo@codesourcery.com>
 
        PR c++/44473
index bb75eda68074a0a14fdb8fa322dc80e66830c9c3..73d35e6c8c4581bd7638a61f2e6b1eae5a594db9 100644 (file)
@@ -1460,7 +1460,12 @@ expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
         zero out the object first.  */
       else if (TYPE_NEEDS_CONSTRUCTING (type))
        {
-         init = build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
+         tree field_size = NULL_TREE;
+         if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
+           /* Don't clobber already initialized virtual bases.  */
+           field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
+         init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
+                                   field_size);
          init = build2 (INIT_EXPR, type, exp, init);
          finish_expr_stmt (init);
          /* And then call the constructor.  */
index a9a593bba174ea53e9487089be5030921c487394..fb50220228a56bc4f882eb561ff432dc6c4fe2a1 100644 (file)
@@ -1,3 +1,8 @@
+2011-10-13  Jason Merrill  <jason@redhat.com>
+
+       PR c++/50618
+       * g++.dg/init/vbase1.C: New.
+
 2011-10-13  Janus Weil  <janus@gcc.gnu.org>
 
        PR fortran/50659
diff --git a/gcc/testsuite/g++.dg/init/vbase1.C b/gcc/testsuite/g++.dg/init/vbase1.C
new file mode 100644 (file)
index 0000000..bbfd58f
--- /dev/null
@@ -0,0 +1,39 @@
+// PR c++/50618
+// { dg-do run }
+
+struct Base
+{
+    const int text;
+    Base():text(1) {}
+    Base(int aText)
+    : text(aText) {}
+};
+struct SubA : public virtual Base
+{
+protected:
+  int x;
+public:
+  SubA(int aX)
+  : x(aX) {}
+};
+class SubB : public virtual Base
+{};
+struct Diamond : public SubA, public SubB
+{
+    Diamond(int text)
+    : Base(text), SubA(5), SubB() {}
+
+    void printText()
+    {
+        if(text != 2)
+          __builtin_abort();
+        if(x!=5)
+          __builtin_abort();
+    }
+};
+
+int main(int, char**)
+{
+    Diamond x(2);
+    x.printText();
+}