Fix issue 10090: ValueFlow: Wrong lifetime, pointer member is not allocated on stack (#3109)

This commit is contained in:
Paul Fultz II 2021-02-03 03:22:31 -06:00 committed by GitHub
parent cf8a5d9a22
commit 0ecac8e23b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -593,6 +593,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
continue; continue;
if (!isLifetimeBorrowed(tok, mSettings)) if (!isLifetimeBorrowed(tok, mSettings))
continue; continue;
if (tokvalue->exprId() == tok->exprId() && !(tok->variable() && tok->variable()->isArray()))
continue;
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) && if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
isInScope(tokvalue->variable()->nameToken(), scope)) || isInScope(tokvalue->variable()->nameToken(), scope)) ||
isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) { isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) {

View File

@ -2579,6 +2579,24 @@ private:
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #10090
check("struct a {\n"
" int b{};\n"
"};\n"
"struct c {\n"
" int* c{};\n"
" a* d{};\n"
"};\n"
"a* f();\n"
"c g() {\n"
" c e;\n"
" e.d = f();\n"
" if (e.d)\n"
" e.c = &e.d->b;\n"
" return e;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void danglingLifetimeFunction() { void danglingLifetimeFunction() {