r295545 - [analyzer] Fix crash in CastToStruct when there is no record definition
Daniel Marjamaki via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 18 03:18:58 PST 2017
Author: danielmarjamaki
Date: Sat Feb 18 05:18:57 2017
New Revision: 295545
URL: http://llvm.org/viewvc/llvm-project?rev=295545&view=rev
Log:
[analyzer] Fix crash in CastToStruct when there is no record definition
This crash was reported in https://bugs.llvm.org//show_bug.cgi?id=31173
Differential Revision: https://reviews.llvm.org/D28297
Modified:
cfe/trunk/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp
cfe/trunk/test/Analysis/cast-to-struct.cpp
Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp?rev=295545&r1=295544&r2=295545&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp Sat Feb 18 05:18:57 2017
@@ -84,6 +84,13 @@ bool CastToStructVisitor::VisitCastExpr(
if (!VD || VD->getType()->isReferenceType())
return true;
+ // Don't warn when target type has no definition.
+ if (const RecordType *RD = dyn_cast<RecordType>(ToPointeeTy.getTypePtr())) {
+ if (!RD->getDecl()->getDefinition()) {
+ return true;
+ }
+ }
+
// Warn when there is widening cast.
unsigned ToWidth = Ctx.getTypeInfo(ToPointeeTy).Width;
unsigned OrigWidth = Ctx.getTypeInfo(OrigPointeeTy).Width;
Modified: cfe/trunk/test/Analysis/cast-to-struct.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/cast-to-struct.cpp?rev=295545&r1=295544&r2=295545&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/cast-to-struct.cpp (original)
+++ cfe/trunk/test/Analysis/cast-to-struct.cpp Sat Feb 18 05:18:57 2017
@@ -65,3 +65,8 @@ void intToStruct(int *P) {
void *VP = P;
Abc = (struct ABC *)VP;
}
+
+// https://llvm.org/bugs/show_bug.cgi?id=31173
+void dontCrash(struct AB X) {
+ struct UndefS *S = (struct UndefS *)&X;
+}
More information about the cfe-commits
mailing list