diff options
-rw-r--r-- | clang/lib/StaticAnalyzer/Core/MemRegion.cpp | 25 | ||||
-rw-r--r-- | clang/test/Analysis/reinterpret-cast.cpp | 15 |
2 files changed, 27 insertions, 13 deletions
diff --git a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp index 632a381a398..ad3f396e39a 100644 --- a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp +++ b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp @@ -1177,6 +1177,7 @@ RegionRawOffset ElementRegion::getAsArrayOffset() const { /// Returns true if \p Base is an immediate base class of \p Child static bool isImmediateBase(const CXXRecordDecl *Child, const CXXRecordDecl *Base) { + assert(Child && "Child must not be null"); // Note that we do NOT canonicalize the base class here, because // ASTRecordLayout doesn't either. If that leads us down the wrong path, // so be it; at least we won't crash. @@ -1256,18 +1257,18 @@ RegionOffset MemRegion::getAsOffset() const { if (!Child) { // We cannot compute the offset of the base class. SymbolicOffsetBase = R; - } - - if (RootIsSymbolic) { - // Base layers on symbolic regions may not be type-correct. - // Double-check the inheritance here, and revert to a symbolic offset - // if it's invalid (e.g. due to a reinterpret_cast). - if (BOR->isVirtual()) { - if (!Child->isVirtuallyDerivedFrom(BOR->getDecl())) - SymbolicOffsetBase = R; - } else { - if (!isImmediateBase(Child, BOR->getDecl())) - SymbolicOffsetBase = R; + } else { + if (RootIsSymbolic) { + // Base layers on symbolic regions may not be type-correct. + // Double-check the inheritance here, and revert to a symbolic offset + // if it's invalid (e.g. due to a reinterpret_cast). + if (BOR->isVirtual()) { + if (!Child->isVirtuallyDerivedFrom(BOR->getDecl())) + SymbolicOffsetBase = R; + } else { + if (!isImmediateBase(Child, BOR->getDecl())) + SymbolicOffsetBase = R; + } } } diff --git a/clang/test/Analysis/reinterpret-cast.cpp b/clang/test/Analysis/reinterpret-cast.cpp index cb7cbfd325d..f3b0a7b257b 100644 --- a/clang/test/Analysis/reinterpret-cast.cpp +++ b/clang/test/Analysis/reinterpret-cast.cpp @@ -102,4 +102,17 @@ int radar_13146953(void) { set_x1(x); set_x2((void *&)y); return *x + *y; // no warning -}
\ No newline at end of file +} + +namespace PR25426 { + struct Base { + int field; + }; + + struct Derived : Base { }; + + void foo(int &p) { + Derived &d = (Derived &)(p); + d.field = 2; + } +} |