[LLVMbugs] [Bug 897] NEW: ADT/SmallVector.h violates aliasing rules

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Tue Sep 5 17:13:08 PDT 2006


           Summary: ADT/SmallVector.h violates aliasing rules
           Product: libraries
           Version: trunk
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Core LLVM classes
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: nicholas at mxc.ca

gcc 4.1.2 emits the following warning on SmallVector:

llvm/include/llvm/ADT/SmallVector.h:46: warning: dereferencing type-punned
pointer will break strict-aliasing rules

This appears to be due to the use of a union:

template <typename T>
class SmallVectorImpl {
  T *Begin, *End, *Capacity;
  // Allocate raw space for N elements of type T.  If T has a ctor or dtor, we
  // don't want it to be automatically run, so we need to represent the space as
  // something else.  An array of char would work great, but might not be
  // aligned sufficiently.  Instead, we either use GCC extensions, or some
  // number of union instances for the space, which guarantee maximal alignment.
  union U {
    double D;
    long double LD;
    long long L;
    void *P;
  } FirstEl;
  // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
  // Default ctor - Initialize to empty.
  SmallVectorImpl(unsigned N)
    : Begin((T*)&FirstEl), End((T*)&FirstEl), Capacity((T*)&FirstEl+N) {

Line 46 being the one with where Begin, End and Capacity are being assigned. The
trouble is that you aren't allowed to put a "T" in the union anyways (otherwise,
that would be a neat hack to preserve the type without going through the

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.

More information about the llvm-bugs mailing list