From 7ebfb10edd4c74e1415c64ad7378e9740f6106f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 13 Nov 2016 18:19:51 +0100 Subject: [PATCH] Fixed #7806 (ValueFlow: better handling of subfunction with early return) --- lib/valueflow.cpp | 9 ++++++--- test/testvalueflow.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 77f4c18d5..0fd26562f 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1279,8 +1279,11 @@ static bool valueFlowForward(Token * const startToken, // goto '}' tok2 = startToken1->link(); - if (condAlwaysTrue && isReturnScope(tok2)) - return false; + if (isReturnScope(tok2)) { + if (condAlwaysTrue) + return false; + removeValues(values, truevalues); + } continue; } @@ -2255,7 +2258,7 @@ static void valueFlowInjectParameter(TokenList* tokenlist, ErrorLogger* errorLog if (!varid2) return; - valueFlowForward(const_cast(functionScope->classStart->next()), functionScope->classEnd, arg, varid2, argvalues, true, tokenlist, errorLogger, settings); + valueFlowForward(const_cast(functionScope->classStart->next()), functionScope->classEnd, arg, varid2, argvalues, false, tokenlist, errorLogger, settings); } static void valueFlowSwitchVariable(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 6a9f91f9a..273d8d0f8 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1716,6 +1716,17 @@ private: "}"; ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); + code = "void f1(int x) {\n" + " if (x == 0) return;\n" + " int y = x;\n" + "}\n" + "\n" + "void f2() {\n" + " f1(x&4);\n" // possible {0,4} + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); + ASSERT_EQUALS(true, testValueOfX(code, 3U, 4)); + code = "void f1(int x) { a=x; }\n" "void f2(int y) { f1(y<123); }\n"; ASSERT_EQUALS(true, testValueOfX(code, 1U, 0));