From ac41b45a5f1949495bad208c69526eafcb8643f0 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sun, 28 May 2023 14:33:12 +0200 Subject: [PATCH] Fix #11729 danglingLifetime of static variable (#5080) --- lib/checkautovariables.cpp | 2 +- test/testautovariables.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index c93bad510..d4a207e04 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -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(), diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index ea81c6989..68ab4f8b5 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -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 v;\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeFunction() {