From e46691597ecf5fe62c34346f46037e8cb2650adc Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sun, 11 Nov 2018 01:04:01 -0600 Subject: [PATCH] Fix issue 8841: FP knownConditionTrueFalse - Dont treat unknown function as noreturn (#1474) * Fix issue 8841: Dont treat unknown function as noreturn * Add a parameter to set default value when the function is unknown --- lib/valueflow.cpp | 12 ++++++++---- test/testcondition.cpp | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 83bf785b4..e3ceb0e15 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -304,13 +304,17 @@ static const Token * skipValueInConditionalExpression(const Token * const valuet return nullptr; } -static bool isEscapeScope(const Token* tok, TokenList * tokenlist) +static bool isEscapeScope(const Token* tok, TokenList * tokenlist, bool unknown = false) { if (!Token::simpleMatch(tok, "{")) return false; const Token * termTok = Token::findmatch(tok, "return|continue|break|throw|goto", tok->link()); - return (termTok && termTok->scope() == tok->scope()) || - (tokenlist && tokenlist->getSettings()->library.isScopeNoReturn(tok->link(), nullptr)); + if(termTok && termTok->scope() == tok->scope()) + return true; + std::string unknownFunction; + if(tokenlist && tokenlist->getSettings()->library.isScopeNoReturn(tok->link(), &unknownFunction)) + return unknownFunction.empty() || unknown; + return false; } static bool bailoutSelfAssignment(const Token * const tok) @@ -4227,7 +4231,7 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold valueFlowContainerReverse(scope.classDef, tok->varId(), value, settings); // possible value after condition - if (!isEscapeScope(scope.bodyStart, tokenlist)) { + if (!isEscapeScope(scope.bodyStart, tokenlist, true)) { const Token *after = scope.bodyEnd; if (Token::simpleMatch(after, "} else {")) after = isEscapeScope(after->tokAt(2), tokenlist) ? nullptr : after->linkAt(2); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index b2a24efc3..cf4104b4e 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -2683,6 +2683,16 @@ private: " return;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void foo(int i) {\n" + " if (i==42)\n" + " {\n" + " bar();\n" + " }\n" + " if (cond && (42==i))\n" + " return;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void multiConditionAlwaysTrue() {