Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12600 FP returnDanglingLifetime with smart pointer member (regression) #6255

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
const Variable* var = tok2->variable();
if (var)
return var->isLocal() || var->isArgument();
if (Token::simpleMatch(tok2, "["))
if (Token::simpleMatch(tok2, "[") && (!Token::simpleMatch(tok2->astParent(), ".") || tok2->astParent() != tok))
return true;
return isTemporary(tok2, library);
});
Expand Down
6 changes: 6 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,12 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:3] -> [test.cpp:4]: (error) Returning pointer to local variable 'ptr' that will be invalid when returning.\n",
errout_str());

check("struct S { std::unique_ptr<int> p; };\n" // #12600
"int* f(const S* s) {\n"
" return s[0].p.get();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void danglingLifetime() {
check("auto f() {\n"
Expand Down
Loading