Skip to content

Commit b2288e5

Browse files
committed
Fixed cppcheck-opensource#6022 (Defect: False positive due to bug in determining bounds of for loop 'for (i = 2; i < 1; ++i)')
1 parent 5cc2e24 commit b2288e5

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,8 @@ static void valueFlowForLoop(TokenList *tokenlist, ErrorLogger *errorLogger, con
13711371
MathLib::bigint num1(0), num2(0), numAfter(0);
13721372

13731373
if (valueFlowForLoop1(tok, &varid, &num1, &num2, &numAfter)) {
1374+
if (num1 > num2)
1375+
continue;
13741376
valueFlowForLoopSimplify(bodyStart, varid, num1, tokenlist, errorLogger, settings);
13751377
valueFlowForLoopSimplify(bodyStart, varid, num2, tokenlist, errorLogger, settings);
13761378
valueFlowForLoopSimplifyAfter(tok, varid, numAfter, tokenlist, errorLogger, settings);

test/testvalueflow.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,14 @@ class TestValueFlow : public TestFixture {
10321032
ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
10331033
ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
10341034

1035+
code = "void f() {\n"
1036+
" for (int x = 2; x < 1; x++)\n"
1037+
" a[x] = 0;\n" // <- not 2
1038+
" b = x;\n" // <- TODO: this is 2
1039+
"}";
1040+
ASSERT_EQUALS(false, testValueOfX(code, 3U, 2));
1041+
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 4U, 2));
1042+
10351043
code = "void f(int a) {\n"
10361044
" for (int x = a; x < 10; x++)\n"
10371045
" a[x] = 0;\n"

0 commit comments

Comments
 (0)