From a42976d656bc61eff72765a4cfab3fc1668f6f39 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Mon, 14 Sep 2020 11:43:11 -0500 Subject: [PATCH] Fix issue 9898: false positive: knownConditionTrueFalse (#2806) --- lib/forwardanalyzer.cpp | 6 ++++-- test/testvalueflow.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 8b8b4c1c4..318c21b43 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -313,11 +313,13 @@ struct ForwardTraversal { if (!scope) return Progress::Break; if (Token::Match(tok->link()->previous(), ")|else {")) { - const bool inElse = Token::simpleMatch(tok->link()->previous(), "else {"); + const Token* tok2 = tok->link()->previous(); + const bool inElse = Token::simpleMatch(tok2, "else {"); + const bool inLoop = inElse ? false : Token::Match(tok2->link()->previous(), "while|for ("); Token* condTok = getCondTokFromEnd(tok); if (!condTok) return Progress::Break; - if (!condTok->hasKnownIntValue()) { + if (!condTok->hasKnownIntValue() || inLoop) { if (!analyzer->lowerToPossible()) return Progress::Break; } else if (condTok->values().front().intvalue == inElse) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 7cc852f39..7b5bdbe07 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -2479,6 +2479,19 @@ private: "}\n"; ASSERT_EQUALS(false, testValueOfXImpossible(code, 8U, 0)); + code = "int foo(int n) {\n" + " if( n>= 8 ) {\n" + " while(true) {\n" + " n -= 8;\n" + " if( n < 8 )\n" + " break;\n" + " }\n" + " int x = n == 0;\n" + " return x;\n" + " }\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 0)); + code = "bool c();\n" "long f() {\n" " bool stop = false;\n"