Fix issue 9275: False positive: Non-local object uses local variable (#2084)

This commit is contained in:
Paul Fultz II 2019-08-14 13:09:33 -05:00 committed by Daniel Marjamäki
parent 831f2009f5
commit 4f76588f98
2 changed files with 14 additions and 0 deletions

View File

@ -595,6 +595,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
} else if (tok->variable() && tok->variable()->declarationId() == tok->varId()) {
var = tok->variable();
}
if (!isLifetimeBorrowed(tok, mSettings))
continue;
if (var && !var->isLocal() && !var->isArgument() && !isVariableChanged(tok->next(), tok->scope()->bodyEnd, var->declarationId(), var->isGlobal(), mSettings, mTokenizer->isCPP())) {
errorDanglngLifetime(tok2, &val);
break;

View File

@ -1912,6 +1912,18 @@ private:
" return nullptr;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #9275
check("struct S {\n"
" void f();\n"
" std::string m;\n"
"}\n"
"void S::f() {\n"
" char buf[1024];\n"
" const char* msg = buf;\n"
" m = msg;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void danglingLifetimeFunction() {