From: Peter Szecsi Date: Sat, 28 Oct 2017 12:19:08 +0000 (+0000) Subject: [analyzer] LoopUnrolling: check the bitwidth of the used numbers (pr34943) X-Git-Tag: llvmorg-6.0.0-rc1~4677 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1496d188a07124f09589cdba0bd8cb8c58ade8be;p=platform%2Fupstream%2Fllvm.git [analyzer] LoopUnrolling: check the bitwidth of the used numbers (pr34943) The loop unrolling feature aims to track the maximum possible steps a loop can make. In order to implement this, it investigates the initial value of the counter variable and the bound number. (It has to be known.) These numbers are used as llvm::APInts, however, it was not checked if their bitwidths are the same which lead to some crashes. This revision solves this problem by extending the "shorter" one (to the length of the "longer" one). For the detailed bug report, see: https://bugs.llvm.org/show_bug.cgi?id=34943 Differential Revision: https://reviews.llvm.org/D38922 llvm-svn: 316830 --- diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp index 98b6ebd..a8c4b05c 100644 --- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp +++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp @@ -208,9 +208,16 @@ bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx, return false; auto CounterVar = Matches[0].getNodeAs("initVarName"); - auto BoundNum = Matches[0].getNodeAs("boundNum")->getValue(); - auto InitNum = Matches[0].getNodeAs("initNum")->getValue(); + llvm::APInt BoundNum = + Matches[0].getNodeAs("boundNum")->getValue(); + llvm::APInt InitNum = + Matches[0].getNodeAs("initNum")->getValue(); auto CondOp = Matches[0].getNodeAs("conditionOperator"); + if (InitNum.getBitWidth() != BoundNum.getBitWidth()) { + InitNum = InitNum.zextOrSelf(BoundNum.getBitWidth()); + BoundNum = BoundNum.zextOrSelf(InitNum.getBitWidth()); + } + if (CondOp->getOpcode() == BO_GE || CondOp->getOpcode() == BO_LE) maxStep = (BoundNum - InitNum + 1).abs().getZExtValue(); else diff --git a/clang/test/Analysis/loop-unrolling.cpp b/clang/test/Analysis/loop-unrolling.cpp index 8ea5b82..844d1f1 100644 --- a/clang/test/Analysis/loop-unrolling.cpp +++ b/clang/test/Analysis/loop-unrolling.cpp @@ -373,3 +373,9 @@ int num_steps_over_limit3() { return 0; } + +void pr34943() { + for (int i = 0; i < 6L; ++i) { + clang_analyzer_numTimesReached(); // expected-warning {{6}} + } +}