From 4b11bb4ad3d4f8becebdc5d553e24f92d94180b6 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 18 May 2021 00:28:45 -0500 Subject: [PATCH] 10278: ValueFlow: Wrong known value, sign conversion (#3260) --- lib/valueflow.cpp | 2 +- test/testcondition.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 169df0b68..fd5585cdd 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1461,7 +1461,7 @@ static std::vector minUnsignedValue(const Token* tok, int depth return result; if (tok->hasKnownIntValue()) { result = {tok->values().front().intvalue}; - } else if (tok->isConstOp() && tok->astOperand1() && tok->astOperand2()) { + } else if (tok->str() != "-" && tok->isConstOp() && tok->astOperand1() && tok->astOperand2()) { std::vector op1 = minUnsignedValue(tok->astOperand1(), depth - 1); std::vector op2 = minUnsignedValue(tok->astOperand2(), depth - 1); if (!op1.empty() && !op2.empty()) { diff --git a/test/testcondition.cpp b/test/testcondition.cpp index c431fcf91..90f661ec1 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3620,6 +3620,12 @@ private: " g(name);\n" "}\n"); ASSERT_EQUALS("[test.cpp:5]: (style) Condition 'name.empty()' is always true\n", errout.str()); + + // #10278 + check("void foo(unsigned int x) {\n" + " if ((100 - x) > 0) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void alwaysTrueInfer() {