From: Gabor Horvath Date: Fri, 13 Apr 2018 12:36:08 +0000 (+0000) Subject: [analyzer] Fix null deref in AnyFunctionCall::getRuntimeDefinition X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=606446ccb7311eb58fac5326f1d52fb510fda146;p=clang [analyzer] Fix null deref in AnyFunctionCall::getRuntimeDefinition Patch by: Rafael Stahl! Differential Revision: https://reviews.llvm.org/D45564 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@330009 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Core/CallEvent.cpp b/lib/StaticAnalyzer/Core/CallEvent.cpp index dab5335ae0..259386cc48 100644 --- a/lib/StaticAnalyzer/Core/CallEvent.cpp +++ b/lib/StaticAnalyzer/Core/CallEvent.cpp @@ -389,23 +389,24 @@ ArrayRef AnyFunctionCall::parameters() const { RuntimeDefinition AnyFunctionCall::getRuntimeDefinition() const { const FunctionDecl *FD = getDecl(); + if (!FD) + return {}; + // Note that the AnalysisDeclContext will have the FunctionDecl with // the definition (if one exists). - if (FD) { - AnalysisDeclContext *AD = - getLocationContext()->getAnalysisDeclContext()-> - getManager()->getContext(FD); - bool IsAutosynthesized; - Stmt* Body = AD->getBody(IsAutosynthesized); - DEBUG({ - if (IsAutosynthesized) - llvm::dbgs() << "Using autosynthesized body for " << FD->getName() - << "\n"; - }); - if (Body) { - const Decl* Decl = AD->getDecl(); - return RuntimeDefinition(Decl); - } + AnalysisDeclContext *AD = + getLocationContext()->getAnalysisDeclContext()-> + getManager()->getContext(FD); + bool IsAutosynthesized; + Stmt* Body = AD->getBody(IsAutosynthesized); + DEBUG({ + if (IsAutosynthesized) + llvm::dbgs() << "Using autosynthesized body for " << FD->getName() + << "\n"; + }); + if (Body) { + const Decl* Decl = AD->getDecl(); + return RuntimeDefinition(Decl); } SubEngine *Engine = getState()->getStateManager().getOwningEngine(); @@ -413,7 +414,7 @@ RuntimeDefinition AnyFunctionCall::getRuntimeDefinition() const { // Try to get CTU definition only if CTUDir is provided. if (!Opts.naiveCTUEnabled()) - return RuntimeDefinition(); + return {}; cross_tu::CrossTranslationUnitContext &CTUCtx = *Engine->getCrossTranslationUnitContext(); diff --git a/test/Analysis/undef-call.c b/test/Analysis/undef-call.c new file mode 100644 index 0000000000..35c0b685ce --- /dev/null +++ b/test/Analysis/undef-call.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -fsyntax-only -analyze -analyzer-checker=debug.ExprInspection -analyzer-config experimental-enable-naive-ctu-analysis=true -analyzer-config ctu-dir=%T/ctudir -verify %s +// expected-no-diagnostics + +struct S { + void (*fp)(); +}; + +int main() { + struct S s; + // This will cause the analyzer to look for a function definition that has + // no FunctionDecl. It used to cause a crash in AnyFunctionCall::getRuntimeDefinition. + // It would only occur when CTU analysis is enabled. + s.fp(); +}