]> granicus.if.org Git - clang/commitdiff
[analyzer] Fix crashes in CastToStruct checker for undefined structs
authorDaniel Marjamaki <daniel.marjamaki@evidente.se>
Tue, 7 Mar 2017 19:20:48 +0000 (19:20 +0000)
committerDaniel Marjamaki <daniel.marjamaki@evidente.se>
Tue, 7 Mar 2017 19:20:48 +0000 (19:20 +0000)
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@297187 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/CastToStructChecker.cpp
test/Analysis/cast-to-struct.cpp

index 16a475ae9dd21e855b08168059cd13e17e9f225d..65e81315f095eceb20b4a1cd424dc33263a72284 100644 (file)
@@ -84,6 +84,10 @@ bool CastToStructVisitor::VisitCastExpr(const CastExpr *CE) {
     if (!VD || VD->getType()->isReferenceType())
       return true;
 
+    if (ToPointeeTy->isIncompleteType() ||
+        OrigPointeeTy->isIncompleteType())
+      return true;
+
     // Warn when there is widening cast.
     unsigned ToWidth = Ctx.getTypeInfo(ToPointeeTy).Width;
     unsigned OrigWidth = Ctx.getTypeInfo(OrigPointeeTy).Width;
index 0a6b8ff0f5db89ae5175fbe5f6e60743ef0a95fc..c3aba023c56e463092a0385ea46827d4d4aea653 100644 (file)
@@ -65,3 +65,17 @@ void intToStruct(int *P) {
   void *VP = P;
   Abc = (struct ABC *)VP;
 }
+
+// https://llvm.org/bugs/show_bug.cgi?id=31173
+void dontCrash1(struct AB X) {
+  struct UndefS *S = (struct UndefS *)&X;
+}
+
+struct S;
+struct T {
+  struct S *P;
+};
+extern struct S Var1, Var2;
+void dontCrash2() {
+  ((struct T *) &Var1)->P = &Var2;
+}