Fix #11729 danglingLifetime of static variable (#5080)

This commit is contained in:
chrchr-github 2023-05-28 14:33:12 +02:00 committed by GitHub
parent 163fb79de1
commit ac41b45a5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -614,7 +614,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
const Token* nextTok = nextAfterAstRightmostLeaf(tok->astTop());
if (!nextTok)
nextTok = tok->next();
if (var && !var->isLocal() && !var->isArgument() &&
if (var && !var->isLocal() && !var->isArgument() && !(val.tokvalue && val.tokvalue->variable() && val.tokvalue->variable()->isStatic()) &&
!isVariableChanged(nextTok,
tok->scope()->bodyEnd,
var->declarationId(),

View File

@ -3273,6 +3273,19 @@ private:
" return { {}, {} };\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #11729
check("struct T {\n"
" void add(int* i) {\n"
" v.push_back(i);\n"
" }\n"
" void f() {\n"
" static int val = 1;\n"
" add(&val);\n"
" }\n"
" std::vector<int*> v;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void danglingLifetimeFunction() {