From 834570e3211f02c3f487d24b7a45a8b25377702b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 20 May 2026 16:40:55 +0200 Subject: [PATCH 1/2] Update checkautovariables.cpp --- lib/checkautovariables.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index fdc5deceefa..2c6f72f419c 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -614,8 +614,11 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token } } } - const bool escape = Token::simpleMatch(tok->astParent(), "throw") || - (Token::simpleMatch(tok->astParent(), "return") && !Function::returnsStandardType(scope->function)); + const Token* retThrow = tok->astParent(); + if (Token::simpleMatch(retThrow, "new")) + retThrow = retThrow->astParent(); + const bool escape = Token::simpleMatch(retThrow, "throw") || + (Token::simpleMatch(retThrow, "return") && !Function::returnsStandardType(scope->function)); std::unordered_set exprs; for (const ValueFlow::Value& val:tok->values()) { if (!val.isLocalLifetimeValue() && !val.isSubFunctionLifetimeValue()) From ba1f8fd183aefe4c6f6cdd355dab25ac677f22b8 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 20 May 2026 16:42:09 +0200 Subject: [PATCH 2/2] Update testautovariables.cpp --- test/testautovariables.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 2474c2fe6a5..073ab1434d7 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -4062,6 +4062,14 @@ class TestAutoVariables : public TestFixture { " struct S s = { .i = 0, true };\n" "}\n"); // don't crash ASSERT_EQUALS("", errout_str()); + + check("struct A { int& r; };\n" // #14772 + "A* f() {\n" + " int x = 0;\n" + " return new A{ x };\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:19] -> [test.cpp:3:9] -> [test.cpp:4:17]: (error) Returning object that points to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n", + errout_str()); } void danglingLifetimeInitList() {