From b469da10699e17f129ccc3388b66de12c577ecf2 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 13 Jan 2021 05:35:43 -0600 Subject: [PATCH] Fix issue 8253: False negative: Comparison is always true (early return) (#3043) --- lib/forwardanalyzer.cpp | 2 -- test/testvalueflow.cpp | 10 ++++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index e48c8b733..8d52d2402 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -386,8 +386,6 @@ struct ForwardTraversal { Token* endBlock = endCond->next()->link(); Token* condTok = getCondTok(tok); Token* initTok = getInitTok(tok); - if (!condTok) - return Progress::Break; if (initTok && updateRecursive(initTok) == Progress::Break) return Progress::Break; if (Token::Match(tok, "for|while (")) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index eb2cf93fb..8a7ffd494 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -4270,6 +4270,16 @@ private: "}\n"; ASSERT_EQUALS(false, testValueOfXKnown(code, 10U, 0)); ASSERT_EQUALS(false, testValueOfXKnown(code, 10U, 1)); + + code = "void f() {\n" + " const int size = arrayInfo.num(0);\n" + " if (size <= 0)\n" + " return;\n" + " for (;;)\n" + " if (size > 0) {}\n" + "}\n"; + ASSERT_EQUALS(true, valueOfTok(code, "> 0").isKnown()); + ASSERT_EQUALS(true, valueOfTok(code, "> 0").intvalue == 1); } static std::string isPossibleContainerSizeValue(const std::list &values, MathLib::bigint i) {