Fix issue 9677: False positive: returning pointer to oject that will not be invalid (#2607)

* Fix issue 9677: False positive: returning pointer to oject that will not be invalid

* Formatting
This commit is contained in:
Paul Fultz II 2020-04-18 02:23:10 -05:00 committed by GitHub
parent aa1bbf2e62
commit 453a69dd8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -465,6 +465,20 @@ static bool isDeadTemporary(bool cpp, const Token* tok, const Token* expr, const
return true;
}
static bool isEscapedReference(const Variable* var)
{
if (!var)
return false;
if (!var->isReference())
return false;
if (!var->declEndToken())
return false;
if (!Token::simpleMatch(var->declEndToken(), "="))
return false;
const Token* vartok = var->declEndToken()->astOperand2();
return !isTemporary(true, vartok, nullptr, false);
}
void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token * end)
{
if (!start)
@ -512,7 +526,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
continue;
if (!isLifetimeBorrowed(tok, mSettings))
continue;
if ((tokvalue->variable() && isInScope(tokvalue->variable()->nameToken(), scope)) ||
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
isInScope(tokvalue->variable()->nameToken(), scope)) ||
isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) {
errorReturnDanglingLifetime(tok, &val);
break;

View File

@ -1647,6 +1647,15 @@ private:
" g(&x);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct Data {\n"
" std::string path;\n"
"};\n"
"const char* foo() {\n"
" const Data& data = getData();\n"
" return data.path.c_str();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void danglingReference() {