[LLVMbugs] [Bug 10304] New: unrestricted unions call *all* constructors/destructors for member types

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Jul 7 14:38:35 PDT 2011


           Summary: unrestricted unions call *all*
                    constructors/destructors for member types
           Product: clang
           Version: trunk
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++0x
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: oneill+llvmbugs at cs.hmc.edu
                CC: llvmbugs at cs.uiuc.edu, dgregor at apple.com

Created an attachment (id=6853)
 --> (http://llvm.org/bugs/attachment.cgi?id=6853)
unionbad.cpp — example of unrestricted union bug

When a union is constructed, generated code calls default constructors for
*all* member types.

Likewise when a union is destroyed, generated code calls *all* destructors.

Back when unions only had POD types (and thus constructors/destructors were all
do-nothing actions, this was harmless, but currently clang allows non-POD types
in unions in c++0x mode, with extremely surprising results.)

> clang++ -std=c++0x -Wall -o unionbad unionbad.cpp
> ./unionbad 
Foo at 0x7fff5fbff218 created
Bar at 0x7fff5fbff218 created
FooBar (union) at 0x7fff5fbff218 created
FooBar (union) at 0x7fff5fbff218 destroyed
Bar at 0x7fff5fbff218 destroyed
Foo at 0x7fff5fbff218 destroyed


> g++-mp-4.6 -std=c++0x -Wall -o unionbad unionbad.cpp
> ./unionbad 
FooBar (union) at 0x7fff5fbff21f created
FooBar (union) at 0x7fff5fbff21f destroyed

P.S.  I realize (now) that unrestricted unions aren't officially supported yet
(despite coppro's comment at r131432), but I think that miscompiling
unsupported c++0x code can lead to subtle and suprising bugs for the unwary.

Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.

More information about the llvm-bugs mailing list