Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const Token*> exprs;
for (const ValueFlow::Value& val:tok->values()) {
if (!val.isLocalLifetimeValue() && !val.isSubFunctionLifetimeValue())
Expand Down
8 changes: 8 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading