From: Peter Szecsi Date: Wed, 21 Feb 2018 16:06:56 +0000 (+0000) Subject: [analyzer] Prevent AnalyzerStatsChecker from crash X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2014a3637b1cb162255a4fd4dafd3a70fc71deae;p=clang [analyzer] Prevent AnalyzerStatsChecker from crash The checker marks the locations where the analyzer creates sinks. However, it can happen that the sink was created because of a loop which does not contain condition statement, only breaks in the body. The exhausted block is the block which should contain the condition but empty, in this case. This change only emits this marking in order to avoid the undefined behavior. Differential Revision: https://reviews.llvm.org/D42266 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325693 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/AnalyzerStatsChecker.cpp b/lib/StaticAnalyzer/Checkers/AnalyzerStatsChecker.cpp index 64c30e7a82..aadc6bac8d 100644 --- a/lib/StaticAnalyzer/Checkers/AnalyzerStatsChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/AnalyzerStatsChecker.cpp @@ -122,6 +122,8 @@ void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G, E = CE.blocks_exhausted_end(); I != E; ++I) { const BlockEdge &BE = I->first; const CFGBlock *Exit = BE.getDst(); + if (Exit->empty()) + continue; const CFGElement &CE = Exit->front(); if (Optional CS = CE.getAs()) { SmallString<128> bufI; diff --git a/test/Analysis/analyzer-stats.c b/test/Analysis/analyzer-stats.c index 5a40d196bb..b58e862f6c 100644 --- a/test/Analysis/analyzer-stats.c +++ b/test/Analysis/analyzer-stats.c @@ -1,4 +1,4 @@ -// RUN: %clang_analyze_cc1 -analyzer-checker=core,deadcode.DeadStores,debug.Stats -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s +// RUN: %clang_analyze_cc1 -analyzer-checker=core,deadcode.DeadStores,debug.Stats -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks -analyzer-max-loop 4 %s int foo(); @@ -12,3 +12,19 @@ int test() { // expected-warning-re{{test -> Total CFGBlocks: {{[0-9]+}} | Unrea a /= 4; return a; } + + +int sink() // expected-warning-re{{sink -> Total CFGBlocks: {{[0-9]+}} | Unreachable CFGBlocks: 1 | Exhausted Block: yes | Empty WorkList: yes}} +{ + for (int i = 0; i < 10; ++i) // expected-warning {{(sink): The analyzer generated a sink at this point}} + ++i; + + return 0; +} + +int emptyConditionLoop() // expected-warning-re{{emptyConditionLoop -> Total CFGBlocks: {{[0-9]+}} | Unreachable CFGBlocks: 0 | Exhausted Block: yes | Empty WorkList: yes}} +{ + int num = 1; + for (;;) + num++; +}