[Bug c++/86849] New: g++ applies guaranteed copy elision to delegating construction, resulting in miscompiles

richard-gccbugzilla at metafoo dot co.uk gcc-bugzilla@gcc.gnu.org
Fri Aug 3 18:47:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86849

            Bug ID: 86849
           Summary: g++ applies guaranteed copy elision to delegating
                    construction, resulting in miscompiles
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: richard-gccbugzilla at metafoo dot co.uk
  Target Milestone: ---

Live testcase: https://godbolt.org/g/AKn7j7

For posterity:

#include <cstring>

struct A {
    A() {}
    A(int);
    ~A() {}

    int n;
    char k;
};

A f();
A::A(int) : A(f()) {}

A f() {
    A result;
    std::memset(&result, 0, sizeof(A));
    return result; // (nrvo)
}

struct B {
    char x, y, z;
};

struct X : A, virtual B { // B is in A's tail padding
    X() : B{1, 2, 3}, A(4) {}
};

char test() {
    X x;
    return x.x; // should return 1
}

Here, GCC uses f() to directly construct the A base class of the X object, and
that tramples over A's tail padding (which contains the B virtual base class of
X, which has already been initialized).

It's not correct to apply guaranteed copy elision to a delegating construction,
just like it's not correct to apply it to a base class construction -- not even
in the C1 constructor variant, due to [[no_unique_address]].


More information about the Gcc-bugs mailing list