From b2288e5ada4e508eabb2c552c412c4bff0efd2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 18 Aug 2014 16:45:22 +0200 Subject: [PATCH] Fixed #6022 (Defect: False positive due to bug in determining bounds of for loop 'for (i = 2; i < 1; ++i)') --- lib/valueflow.cpp | 2 ++ test/testvalueflow.cpp | 8 ++++++++ 2 files changed, 10 insertions(+) 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"