From c5838a86a934b0f9ca9c1365c7e8a868de0bbbbd Mon Sep 17 00:00:00 2001 From: Daniel Marjamaki Date: Sat, 18 Feb 2017 11:18:57 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@295545 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp | 7 +++++++ test/Analysis/cast-to-struct.cpp | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp b/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp index 16a475ae9d..c2880bc215 100644 --- a/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp @@ -84,6 +84,13 @@ bool CastToStructVisitor::VisitCastExpr(const CastExpr *CE) { if (!VD || VD->getType()->isReferenceType()) return true; + // Don't warn when target type has no definition. + if (const RecordType *RD = dyn_cast(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; diff --git a/test/Analysis/cast-to-struct.cpp b/test/Analysis/cast-to-struct.cpp index 45d55947c9..6c23c2bf48 100644 --- a/test/Analysis/cast-to-struct.cpp +++ b/test/Analysis/cast-to-struct.cpp @@ -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; +} -- 2.40.0