Skip to content

Commit 0832830

Browse files
authored
Fix issue 9721: ValueFlow: Comparison is always false, but ValueFlow says it is always true (cppcheck-opensource#2658)
1 parent 4270819 commit 0832830

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

lib/valueflow.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4321,9 +4321,19 @@ static void valueFlowInferCondition(TokenList* tokenlist,
43214321
} else if (tok->isComparisonOp()) {
43224322
MathLib::bigint val = 0;
43234323
const Token* varTok = nullptr;
4324+
std::string op = tok->str();
43244325
if (tok->astOperand1()->hasKnownIntValue()) {
43254326
val = tok->astOperand1()->values().front().intvalue;
43264327
varTok = tok->astOperand2();
4328+
// Flip the operator
4329+
if (op == ">")
4330+
op = "<";
4331+
else if (op == "<")
4332+
op = ">";
4333+
else if (op == ">=")
4334+
op = "<=";
4335+
else if (op == "<=")
4336+
op = ">=";
43274337
} else if (tok->astOperand2()->hasKnownIntValue()) {
43284338
val = tok->astOperand2()->values().front().intvalue;
43294339
varTok = tok->astOperand1();
@@ -4336,22 +4346,22 @@ static void valueFlowInferCondition(TokenList* tokenlist,
43364346
continue;
43374347
const ValueFlow::Value* result = nullptr;
43384348
bool known = false;
4339-
if (Token::Match(tok, "==|!=")) {
4349+
if (op == "==" || op == "!=") {
43404350
result = proveNotEqual(varTok->values(), val);
4341-
known = tok->str() == "!=";
4342-
} else if (Token::Match(tok, "<|>=")) {
4351+
known = op == "!=";
4352+
} else if (op == "<" || op == ">=") {
43434353
result = proveLessThan(varTok->values(), val);
4344-
known = tok->str() == "<";
4354+
known = op == "<";
43454355
if (!result && !isSaturated(val)) {
43464356
result = proveGreaterThan(varTok->values(), val - 1);
4347-
known = tok->str() == ">=";
4357+
known = op == ">=";
43484358
}
4349-
} else if (Token::Match(tok, ">|<=")) {
4359+
} else if (op == ">" || op == "<=") {
43504360
result = proveGreaterThan(varTok->values(), val);
4351-
known = tok->str() == ">";
4361+
known = op == ">";
43524362
if (!result && !isSaturated(val)) {
43534363
result = proveLessThan(varTok->values(), val + 1);
4354-
known = tok->str() == "<=";
4364+
known = op == "<=";
43554365
}
43564366
}
43574367
if (!result)

test/testcondition.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,6 +3507,15 @@ class TestCondition : public TestFixture {
35073507
" return pos;\n"
35083508
"}\n");
35093509
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (style) Condition 'pos>0' is always true\n", errout.str());
3510+
3511+
// #9721
3512+
check("void f(int x) {\n"
3513+
" if (x > 127) {\n"
3514+
" if ( (x>255) || (-128>x) )\n"
3515+
" return;\n"
3516+
" }\n"
3517+
"}\n");
3518+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Condition '-128>x' is always false\n", errout.str());
35103519
}
35113520

35123521
void alwaysTrueContainer() {

0 commit comments

Comments
 (0)