From: Richard Trieu Date: Tue, 6 Dec 2011 04:48:01 +0000 (+0000) Subject: Switch a cast to a dyn_cast and check the pointer before using. Fixes a crash X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=26b45d86085a125af036dbcf85dad3087b664ab2;p=clang Switch a cast to a dyn_cast and check the pointer before using. Fixes a crash 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 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 1ec7e39398..869922faf4 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -3771,10 +3771,11 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, } if (D && !D->isWeak()) { - FunctionDecl* F = cast(D); - S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) - << F << E->getSourceRange() << SourceRange(CC); - return; + if (FunctionDecl* F = dyn_cast(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. diff --git a/test/SemaCXX/condition.cpp b/test/SemaCXX/condition.cpp index 21671fab3c..7854d51bfd 100644 --- a/test/SemaCXX/condition.cpp +++ b/test/SemaCXX/condition.cpp @@ -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); +}