Index: clang/lib/StaticAnalyzer/Checkers/TrustReturnsNonnullChecker.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/TrustReturnsNonnullChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/TrustReturnsNonnullChecker.cpp @@ -1,5 +1,4 @@ -//== TrustReturnsNonnullChecker.cpp --------- API nullability modeling -*- C++ -//-*--==// +//== TrustReturnsNonnullChecker.cpp --------- API nullability modeling -*- C++ -*--==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -13,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "clang/AST/Attr.h" +#include "clang/AST/Decl.h" #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" @@ -30,7 +30,7 @@ void checkPostCall(const CallEvent &Call, CheckerContext &C) const { ProgramStateRef State = C.getState(); - if (isNonNullPtr(Call, C)) + if (isNonNullPtr(Call)) if (auto L = Call.getReturnValue().getAs()) State = State->assume(*L, /*assumption=*/true); @@ -39,16 +39,13 @@ private: /// \returns Whether the method declaration has the attribute returns_nonnull. - bool isNonNullPtr(const CallEvent &Call, CheckerContext &C) const { + bool isNonNullPtr(const CallEvent &Call) const { QualType ExprRetType = Call.getResultType(); - if (!ExprRetType->isAnyPointerType()) + const Decl *CallDeclaration = Call.getDecl(); + if (!ExprRetType->isAnyPointerType() || !CallDeclaration) return false; - if (Call.getDecl()->hasAttr()) { - return true; - } - - return false; + return CallDeclaration->hasAttr(); } }; Index: clang/test/Analysis/test-decl-crash.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/test-decl-crash.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=core,apiModeling.TrustReturnsNonnull -verify %s + +// expected-no-diagnostics + +void test(void *(*f)(void)) { + // will probably crash the compiler, worth a test case + f(); +} \ No newline at end of file