From 735f716603694581b9013e7cff8c4717058923c9 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 4 Aug 2021 14:07:55 -0500 Subject: [PATCH] Fix 10394 and 10395: FP knownConditionTrueFalse: loop variable (#3381) --- lib/valueflow.cpp | 2 ++ test/testvalueflow.cpp | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 40d5fea7a..211fd59ab 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4113,6 +4113,8 @@ static void valueFlowSymbolic(TokenList* tokenlist, SymbolDatabase* symboldataba for (Token* tok = const_cast(scope->bodyStart); tok != scope->bodyEnd; tok = tok->next()) { if (!Token::simpleMatch(tok, "=")) continue; + if (tok->astParent()) + continue; if (!tok->astOperand1()) continue; if (!tok->astOperand2()) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 5e0a63f15..f0a8f3a28 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1206,10 +1206,7 @@ private: " for (x = a; x < 50; x++) {}\n" " b = x;\n" "}\n"; - ASSERT_EQUALS("3,x is assigned 'a' here.\n" - "3,x is incremented', new value is symbolic=a+1\n" - "3,x is incremented', new value is symbolic=a+2\n" - "3,After for loop, x has value 50\n", + ASSERT_EQUALS("3,After for loop, x has value 50\n", getErrorPathForX(code, 4U)); } @@ -6021,6 +6018,17 @@ private: " return x;\n" "}\n"; ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, 1)); + + code = "int f(int i) {\n" + " for(int j = i;;j++) {\n" + " int x = j;\n" + " return x;\n" + " }\n" + " return 0;\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, "i", 0)); + ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, "i", 1)); + ASSERT_EQUALS(true, testValueOfXKnown(code, 4U, "j", 0)); } };