Skip to content

Commit ac41b45

Browse files
Fix #11729 danglingLifetime of static variable (#5080)
1 parent 163fb79 commit ac41b45

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/checkautovariables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
614614
const Token* nextTok = nextAfterAstRightmostLeaf(tok->astTop());
615615
if (!nextTok)
616616
nextTok = tok->next();
617-
if (var && !var->isLocal() && !var->isArgument() &&
617+
if (var && !var->isLocal() && !var->isArgument() && !(val.tokvalue && val.tokvalue->variable() && val.tokvalue->variable()->isStatic()) &&
618618
!isVariableChanged(nextTok,
619619
tok->scope()->bodyEnd,
620620
var->declarationId(),

test/testautovariables.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,6 +3273,19 @@ class TestAutoVariables : public TestFixture {
32733273
" return { {}, {} };\n"
32743274
"}\n");
32753275
ASSERT_EQUALS("", errout.str());
3276+
3277+
// #11729
3278+
check("struct T {\n"
3279+
" void add(int* i) {\n"
3280+
" v.push_back(i);\n"
3281+
" }\n"
3282+
" void f() {\n"
3283+
" static int val = 1;\n"
3284+
" add(&val);\n"
3285+
" }\n"
3286+
" std::vector<int*> v;\n"
3287+
"};\n");
3288+
ASSERT_EQUALS("", errout.str());
32763289
}
32773290

32783291
void danglingLifetimeFunction() {

0 commit comments

Comments
 (0)