Skip to content

Commit 31337dd

Browse files
committed
ValueFlow: Better handling of && and || in for loop to avoid FP
1 parent d4f2512 commit 31337dd

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,8 +2301,8 @@ static void valueFlowForLoopSimplify(Token * const bodyStart, const unsigned int
23012301

23022302
if (Token::Match(tok2, "%oror%|&&")) {
23032303
const ProgramMemory programMemory(getProgramMemory(tok2->astTop(), varid, ValueFlow::Value(value)));
2304-
if ((tok2->str() == "&&" && conditionIsFalse(tok2->astOperand1(), programMemory)) ||
2305-
(tok2->str() == "||" && conditionIsTrue(tok2->astOperand1(), programMemory))) {
2304+
if ((tok2->str() == "&&" && !conditionIsTrue(tok2->astOperand1(), programMemory)) ||
2305+
(tok2->str() == "||" && !conditionIsFalse(tok2->astOperand1(), programMemory))) {
23062306
// Skip second expression..
23072307
const Token *parent = tok2;
23082308
while (parent && parent->str() == tok2->str())

test/testvalueflow.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,14 @@ class TestValueFlow : public TestFixture {
17311731
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
17321732
ASSERT_EQUALS(true, testValueOfX(code, 4U, 9));
17331733

1734+
code = "void foo() {\n"
1735+
" for (int x = 0; x < 10; x++) {\n"
1736+
" if (x < value\n"
1737+
" && x) {}" // <- maybe x is not 9
1738+
" }\n"
1739+
"}\n";
1740+
ASSERT_EQUALS(false, testValueOfX(code, 4U, 9));
1741+
17341742
// ||
17351743
code = "void foo() {\n"
17361744
" for (int x = 0; x < 10; x++) {\n"

0 commit comments

Comments
 (0)