]> 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:03:06 +0000 (14:03 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Thu, 13 Oct 2011 18:03:06 +0000 (14:03 -0400)
PR c++/50618
* init.c (expand_aggr_init_1): Don't zero-initialize virtual
bases of a base subobject.

From-SVN: r179938

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

index a5a834187d4efa7306e4378d4bfa437c4205317b..3797b3a7a2708445b90ba43f0fe86aa23a763297 100644 (file)
@@ -1,5 +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.
+
        Backported from 4.6 branch
        2011-03-11  Jakub Jelinek  <jakub@redhat.com>
 
index 4294acc80653d1926b8ffcfd41ea0cf2f95f4cd3..d219a4f176dfdcfc9ba7bec04009807aad634e44 100644 (file)
@@ -1443,7 +1443,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 3fe28e698bbfb07b294be831b84f2ebce13b80f8..8ccbd7ebc5ceeaded33c93de4c33d1777cb7b486 100644 (file)
@@ -1,5 +1,8 @@
 2011-10-13  Jason Merrill  <jason@redhat.com>
 
+       PR c++/50618
+       * g++.dg/init/vbase1.C: New.
+
        Backported from 4.6 branch
        2011-03-11  Jakub Jelinek  <jakub@redhat.com>
 
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();
+}