From 3329e2f63368cc094cfe53866a929f9de6ee8ca1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 16 Dec 2023 17:03:56 +0100 Subject: [PATCH] Fix #11741 FP knownConditionTrueFalse minus/negation confuses check (#5766) --- lib/valueflow.cpp | 2 +- test/testcondition.cpp | 8 ++++++++ test/testvalueflow.cpp | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6cf189e24..04db01b8f 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6304,7 +6304,7 @@ struct ConditionHandler { values.insert(values.end(), cond.false_values.cbegin(), cond.false_values.cend()); // extra logic for unsigned variables 'i>=1' => possible value can also be 0 - if (Token::Match(tok, "<|>")) { + if (Token::Match(tok, "<|>|<=|>=")) { values.remove_if([](const ValueFlow::Value& v) { if (v.isIntValue()) return v.intvalue != 0; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 2c3a77e3a..b2dc0da31 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4547,6 +4547,14 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("int f(int i) {\n" // #11741 + " i = -i - 1;\n" + " if (i < 0 || i >= 20)\n" + " return 0;\n" + " return 1;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void alwaysTrueSymbolic() diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index df9f50dcd..4eac02e21 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1460,7 +1460,7 @@ private: " int a = x;\n" " if (x >= 1) {}\n" "}"; - ASSERT_EQUALS(true, testValueOfX(code, 2U, 1)); + TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 2U, 1)); ASSERT_EQUALS(true, testValueOfX(code, 2U, 0)); code = "void f(unsigned int x) {\n"