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:
parent
aa1bbf2e62
commit
453a69dd8c
|
@ -465,6 +465,20 @@ static bool isDeadTemporary(bool cpp, const Token* tok, const Token* expr, const
|
||||||
return true;
|
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)
|
void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token * end)
|
||||||
{
|
{
|
||||||
if (!start)
|
if (!start)
|
||||||
|
@ -512,7 +526,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
|
||||||
continue;
|
continue;
|
||||||
if (!isLifetimeBorrowed(tok, mSettings))
|
if (!isLifetimeBorrowed(tok, mSettings))
|
||||||
continue;
|
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)) {
|
isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) {
|
||||||
errorReturnDanglingLifetime(tok, &val);
|
errorReturnDanglingLifetime(tok, &val);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1647,6 +1647,15 @@ private:
|
||||||
" g(&x);\n"
|
" g(&x);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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() {
|
void danglingReference() {
|
||||||
|
|
Loading…
Reference in New Issue