Skip to content

Commit 807b653

Browse files
committed
ValueFlow: Fix FP in for loops when 2nd expression is 0
1 parent f1ad736 commit 807b653

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,8 @@ static bool valueFlowForLoop2(const Token *tok,
23512351
if (error)
23522352
return false;
23532353
execute(secondExpression, &programMemory, &result, &error);
2354+
if (result == 0) // 2nd expression is false => no looping
2355+
return false;
23542356
if (error) {
23552357
// If a variable is reassigned in second expression, return false
23562358
std::stack<const Token *> tokens;

test/testvalueflow.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,14 @@ class TestValueFlow : public TestFixture {
16621662
ASSERT_EQUALS(false, testValueOfX(code, 4U, 2));
16631663
ASSERT_EQUALS(true, testValueOfX(code, 5U, 2));
16641664

1665+
code = "enum AB {A,B};\n" // enum => handled by valueForLoop2
1666+
"void f() {\n"
1667+
" int x;\n"
1668+
" for (x = 1; x < B; ++x)\n"
1669+
" a[x] = 0;\n" // <- not 1
1670+
"}";
1671+
ASSERT_EQUALS(false, testValueOfX(code, 5U, 1));
1672+
16651673
code = "void f(int a) {\n"
16661674
" for (int x = a; x < 10; x++)\n"
16671675
" a[x] = 0;\n"
@@ -1733,7 +1741,7 @@ class TestValueFlow : public TestFixture {
17331741
" for (int x = 0; x < 10 && y = do_something();)\n"
17341742
" x;\n"
17351743
"}";
1736-
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0));
1744+
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 4U, 0));
17371745

17381746
code = "void f() {\n"
17391747
" int x,y;\n"

0 commit comments

Comments
 (0)