From: George Karpenkov Date: Mon, 24 Sep 2018 21:20:30 +0000 (+0000) Subject: [analyzer] Prevent crashes in FindLastStoreBRVisitor X-Git-Tag: llvmorg-8.0.0-rc1~8063 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2a6deeb9288708ce8fb1d62a48823b8236f5251e;p=platform%2Fupstream%2Fllvm.git [analyzer] Prevent crashes in FindLastStoreBRVisitor This patch is a band-aid. A proper solution would be too change trackNullOrUndefValue to only try to dereference the pointer when it is relevant to the problem. Differential Revision: https://reviews.llvm.org/D52435 llvm-svn: 342920 --- diff --git a/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h b/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h index da019f83..6722a0a 100644 --- a/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h +++ b/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h @@ -83,6 +83,8 @@ public: BugReport &BR); }; +/// Finds last store into the given region, +/// which is different from a given symbolic value. class FindLastStoreBRVisitor final : public BugReporterVisitor { const MemRegion *R; SVal V; diff --git a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp index 461e64e..bb93d096 100644 --- a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp +++ b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp @@ -1294,8 +1294,7 @@ FindLastStoreBRVisitor::VisitNode(const ExplodedNode *Succ, if (const auto *BDR = dyn_cast_or_null(V.getAsRegion())) { if (const VarRegion *OriginalR = BDR->getOriginalRegion(VR)) { - if (Optional KV = - State->getSVal(OriginalR).getAs()) + if (auto KV = State->getSVal(OriginalR).getAs()) BR.addVisitor(llvm::make_unique( *KV, OriginalR, EnableNullFPSuppression)); } @@ -1752,8 +1751,18 @@ bool bugreporter::trackNullOrUndefValue(const ExplodedNode *N, else RVal = state->getSVal(L->getRegion()); - if (auto KV = RVal.getAs()) - report.addVisitor(llvm::make_unique( + // FIXME: this is a hack for fixing a later crash when attempting to + // dereference a void* pointer. + // We should not try to dereference pointers at all when we don't care + // what is written inside the pointer. + bool ShouldFindLastStore = true; + if (const auto *SR = dyn_cast(L->getRegion())) + if (SR->getSymbol()->getType()->getPointeeType()->isVoidType()) + ShouldFindLastStore = false; + + if (ShouldFindLastStore) + if (auto KV = RVal.getAs()) + report.addVisitor(llvm::make_unique( *KV, L->getRegion(), EnableNullFPSuppression)); const MemRegion *RegionRVal = RVal.getAsRegion(); diff --git a/clang/test/Analysis/diagnostics/find_last_store.c b/clang/test/Analysis/diagnostics/find_last_store.c new file mode 100644 index 0000000..9bf601e --- /dev/null +++ b/clang/test/Analysis/diagnostics/find_last_store.c @@ -0,0 +1,17 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=text -verify %s +typedef struct { float b; } c; +void *a(); +void *d() { + return a(); // expected-note{{Returning pointer}} +} + +void no_find_last_store() { + c *e = d(); // expected-note{{Calling 'd'}} + // expected-note@-1{{Returning from 'd'}} + // expected-note@-2{{'e' initialized here}} + + (void)(e || e->b); // expected-note{{Assuming 'e' is null}} + // expected-note@-1{{Left side of '||' is false}} + // expected-note@-2{{Access to field 'b' results in a dereference of a null pointer (loaded from variable 'e')}} + // expected-warning@-3{{Access to field 'b' results in a dereference of a null pointer (loaded from variable 'e')}} +}