]> granicus.if.org Git - clang/commitdiff
Switch a cast to a dyn_cast and check the pointer before using. Fixes a crash
authorRichard Trieu <rtrieu@google.com>
Tue, 6 Dec 2011 04:48:01 +0000 (04:48 +0000)
committerRichard Trieu <rtrieu@google.com>
Tue, 6 Dec 2011 04:48:01 +0000 (04:48 +0000)
in the following code:

void test4(bool (&x)(void)) {
  while (x);
}

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145918 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/condition.cpp

index 1ec7e39398b64a489fdf4e76abc6c930fc392f92..869922faf4215784e2543d383f9adf17cad45cf7 100644 (file)
@@ -3771,10 +3771,11 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
       }
 
       if (D && !D->isWeak()) {
-        FunctionDecl* F = cast<FunctionDecl>(D);
-        S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool)
-          << F << E->getSourceRange() << SourceRange(CC);
-        return;
+        if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) {
+          S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool)
+            << F << E->getSourceRange() << SourceRange(CC);
+          return;
+        }
       }
     }
     return; // Other casts to bool are not checked.
index 21671fab3c04728c8feb21f59c1907eeb5638bb4..7854d51bfd7649db5bb86b8565250ebf787b8c4a 100644 (file)
@@ -52,3 +52,7 @@ void test3() {
   if (test3) // expected-warning {{address of function 'test3' will always evaluate to 'true'}}
     (void) 0;
 }
+
+void test4(bool (&x)(void)) {
+  while (x);
+}