From 807b6537396c9b3089053c71ecdee503bc074c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 20 Dec 2016 07:54:38 +0100 Subject: [PATCH] ValueFlow: Fix FP in for loops when 2nd expression is 0 --- lib/valueflow.cpp | 2 ++ test/testvalueflow.cpp | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 0dbb166c3..6505ce459 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2351,6 +2351,8 @@ static bool valueFlowForLoop2(const Token *tok, if (error) return false; execute(secondExpression, &programMemory, &result, &error); + if (result == 0) // 2nd expression is false => no looping + return false; if (error) { // If a variable is reassigned in second expression, return false std::stack tokens; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 840ec8041..8e4b264af 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1662,6 +1662,14 @@ private: ASSERT_EQUALS(false, testValueOfX(code, 4U, 2)); ASSERT_EQUALS(true, testValueOfX(code, 5U, 2)); + code = "enum AB {A,B};\n" // enum => handled by valueForLoop2 + "void f() {\n" + " int x;\n" + " for (x = 1; x < B; ++x)\n" + " a[x] = 0;\n" // <- not 1 + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 5U, 1)); + code = "void f(int a) {\n" " for (int x = a; x < 10; x++)\n" " a[x] = 0;\n" @@ -1733,7 +1741,7 @@ private: " for (int x = 0; x < 10 && y = do_something();)\n" " x;\n" "}"; - ASSERT_EQUALS(true, testValueOfX(code, 4U, 0)); + TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 4U, 0)); code = "void f() {\n" " int x,y;\n"