From 31337dda27a002d807d2977885b7ad19d3a26292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 11 Dec 2016 21:19:24 +0100 Subject: [PATCH] ValueFlow: Better handling of && and || in for loop to avoid FP --- lib/valueflow.cpp | 4 ++-- test/testvalueflow.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 58008c3f7..3b854eb36 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2301,8 +2301,8 @@ static void valueFlowForLoopSimplify(Token * const bodyStart, const unsigned int if (Token::Match(tok2, "%oror%|&&")) { const ProgramMemory programMemory(getProgramMemory(tok2->astTop(), varid, ValueFlow::Value(value))); - if ((tok2->str() == "&&" && conditionIsFalse(tok2->astOperand1(), programMemory)) || - (tok2->str() == "||" && conditionIsTrue(tok2->astOperand1(), programMemory))) { + if ((tok2->str() == "&&" && !conditionIsTrue(tok2->astOperand1(), programMemory)) || + (tok2->str() == "||" && !conditionIsFalse(tok2->astOperand1(), programMemory))) { // Skip second expression.. const Token *parent = tok2; while (parent && parent->str() == tok2->str()) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index c73d8e9ea..328b3ecd8 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1731,6 +1731,14 @@ private: ASSERT_EQUALS(false, testValueOfX(code, 4U, 0)); ASSERT_EQUALS(true, testValueOfX(code, 4U, 9)); + code = "void foo() {\n" + " for (int x = 0; x < 10; x++) {\n" + " if (x < value\n" + " && x) {}" // <- maybe x is not 9 + " }\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfX(code, 4U, 9)); + // || code = "void foo() {\n" " for (int x = 0; x < 10; x++) {\n"