From a0c37ceba27179496fa2f44072f85e2a5448216e Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 21 Aug 2021 14:17:05 -0500 Subject: [PATCH] Fix FP: Set correct bounds for interval (#3411) --- lib/valueflow.cpp | 4 ++-- test/testcondition.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 860875963..348b71ec6 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4545,14 +4545,14 @@ static std::vector infer(const ValuePtr& model, if (!diff.minvalue.empty()) { ValueFlow::Value value(diff.minvalue.front() - 1); value.setImpossible(); - value.bound = ValueFlow::Value::Bound::Lower; + value.bound = ValueFlow::Value::Bound::Upper; addToErrorPath(value, diff.minRef); result.push_back(value); } if (!diff.maxvalue.empty()) { ValueFlow::Value value(diff.maxvalue.front() + 1); value.setImpossible(); - value.bound = ValueFlow::Value::Bound::Upper; + value.bound = ValueFlow::Value::Bound::Lower; addToErrorPath(value, diff.maxRef); result.push_back(value); } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 417aab5b8..cbee6cc8a 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3787,6 +3787,14 @@ private: check("bool f(bool a, bool b) { if(a && b && (!a)){} }"); ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:1]: (style) Condition '!a' is always false\n", errout.str()); + check("void f(int x, int y) {\n" + " if (x < y) {\n" + " auto z = y - x;\n" + " if (z < 1) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'z<1' is always false\n", errout.str()); + check("struct a {\n" " a *b() const;\n" "} c;\n"