[LLVMbugs] [Bug 13824] New: Warn about use of reinterpret_cast for upcasts or downcasts

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Sep 12 13:33:27 PDT 2012


http://llvm.org/bugs/show_bug.cgi?id=13824

             Bug #: 13824
           Summary: Warn about use of reinterpret_cast for upcasts or
                    downcasts
           Product: clang
           Version: trunk
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: jordan_rose at apple.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified


The static analyzer recently hit an assertion on some code that used
reinterpret_cast to perform a downcast. In C++, using reinterpret_cast to
perform a downcast or upcast is very different from using static_cast, since a
pointer may require adjustment to point to the correct subobject. The source we
were analyzing should have just used static_cast.

Here is a simple example that does not warn with ToT today, and results in a
segfault when compiled and run on OS X:


struct A { int x; };

struct B : public A {
    virtual int foo() { return x; }
};

int main() {
    B *b = new B();
        b->x = 42;
    A *a = b;
    B *ptr = reinterpret_cast<B *>(a); // bad idea!
    return ptr->foo();
}

-- 
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