Fix FP when returning a pointer to a container (#3379)

This commit is contained in:
Paul Fultz II 2021-08-04 01:16:31 -05:00 committed by GitHub
parent 28c8b00e5c
commit 4626f9ed76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -2838,7 +2838,8 @@ static std::vector<LifetimeToken> getLifetimeTokens(const Token* tok,
if (!vartok)
return {{tok, std::move(errorPath)}};
const Variable *tokvar = vartok->variable();
if (!isUniqueSmartPointer(vartok) && !astIsContainer(vartok) &&
const bool isContainer = astIsContainer(vartok) && !astIsPointer(vartok);
if (!isUniqueSmartPointer(vartok) && !isContainer &&
!(tokvar && tokvar->isArray() && !tokvar->isArgument()) &&
(Token::Match(vartok->astParent(), "[|*") || vartok->astParent()->originalName() == "->")) {
for (const ValueFlow::Value &v : vartok->values()) {

View File

@ -118,6 +118,7 @@ private:
TEST_CASE(returnReference20); // #9536
TEST_CASE(returnReference21); // #9530
TEST_CASE(returnReference22);
TEST_CASE(returnReference23);
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
@ -1451,6 +1452,14 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Reference to local variable returned.\n", errout.str());
}
void returnReference23() {
check("const std::vector<int> * g();\n"
"const std::vector<int>& f() {\n"
" return *g();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"