diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f0df97834..cf70d5937 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1371,6 +1371,8 @@ static void valueFlowForLoop(TokenList *tokenlist, ErrorLogger *errorLogger, con MathLib::bigint num1(0), num2(0), numAfter(0); if (valueFlowForLoop1(tok, &varid, &num1, &num2, &numAfter)) { + if (num1 > num2) + continue; valueFlowForLoopSimplify(bodyStart, varid, num1, tokenlist, errorLogger, settings); valueFlowForLoopSimplify(bodyStart, varid, num2, tokenlist, errorLogger, settings); valueFlowForLoopSimplifyAfter(tok, varid, numAfter, tokenlist, errorLogger, settings); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index e134a55ed..872f915ff 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1032,6 +1032,14 @@ private: ASSERT_EQUALS(true, testValueOfX(code, 3U, 9)); ASSERT_EQUALS(false, testValueOfX(code, 3U, 10)); + code = "void f() {\n" + " for (int x = 2; x < 1; x++)\n" + " a[x] = 0;\n" // <- not 2 + " b = x;\n" // <- TODO: this is 2 + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 3U, 2)); + TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 4U, 2)); + code = "void f(int a) {\n" " for (int x = a; x < 10; x++)\n" " a[x] = 0;\n"