[LLVMbugs] [Bug 23373] New: Miscompile of defaulted union copy ctor

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Apr 29 05:28:46 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=23373

            Bug ID: 23373
           Summary: Miscompile of defaulted union copy ctor
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: dario.domizioli at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Contrived example:

// ---------------------------------------------------------------------------
int testflag = 0;

int main() {

    union U {
        int data;              // Some data member.
        U(U&) = default;       // Note the non-const &. It is allowed by the
                               // standard. A non-deleted, defaulted copy ctor
                               // should copy the data representation of the
                               // union.
        U(int u) { data = u; } // Constructor from int.
    } obj1(2), obj2(obj1);     // Obj2 must be initialized after obj1, so it
                               // should contain 2.

    if(obj2.data != 2) {       // This should be false (i.e. obj2.data == 2)!
        testflag++;            // This should never be executed, but it is!
    }
    return testflag;
}
// ---------------------------------------------------------------------------

Compile and link with just: clang -std=c++11 <file>
I am using the x86_64-unknown-linux triple.

Even when compiled at -O0, testflag is incremented and this code returns with
an exit code of 1.
I believe the implementation of the explicitly-defaulted, implicitly-defined,
non-const-& version of the copy ctor for union U is miscompiled.

I've looked at my copy of the C++11 standard (INCITS+ISO+IEC+14882-2012) and I
think the code is OK.

The copy ctor is allowed to have a non-const reference and it's still a copy
ctor (Clause 12.8 paragraph 2).
An explicitly defaulted copy ctor is implicitly defined in this case (Clause
12.8 paragraph 13).
An implicitly defined copy ctor for a union copies the object representation of
the union (Clause 12.8 paragraph 16) so it should copy the data.
The initialization of "obj2" must happen after "obj1" because of Clause 8,
paragraph 3 (and footnote 97 for clarification).

I have also noticed that the miscompile does not seem to happen when the
defaulted copy ctor is the "const&" variant.
    U(U const&) = default; // This would work fine.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150429/734eab81/attachment.html>


More information about the llvm-bugs mailing list