Skip to content

Commit

Permalink
Fix 12600: FP returnDanglingLifetime with smart pointer member (regre…
Browse files Browse the repository at this point in the history
…ssion)
  • Loading branch information
pfultz2 committed Apr 9, 2024
1 parent 3ad4533 commit 3135907
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,16 +653,23 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
return tok;
// If any of the submembers are borrowed types then stop
if (std::any_of(it.base() - 1, members.cend() - 1, [&](const Token* tok2) {
if (astIsPointer(tok2) || astIsContainerView(tok2) || astIsIterator(tok2))
const Token* obj = tok2;
if(Token::simpleMatch(obj, "["))
obj = tok2->astOperand1();
const Variable* var = obj->variable();
// Check for arrays first since astIsPointer will return true, but an array is not a borrowed type
if(var && var->isArray())
return false;
if (astIsPointer(obj) || astIsContainerView(obj) || astIsIterator(obj))
return true;
if (!astIsUniqueSmartPointer(tok2)) {
if (astIsSmartPointer(tok2))
if (!astIsUniqueSmartPointer(obj)) {
if (astIsSmartPointer(obj))
return true;
const Token* dotTok = tok2->next();
const Token* dotTok = obj->next();
if (!Token::simpleMatch(dotTok, ".")) {
const Token* endTok = nextAfterAstRightmostLeaf(tok2);
const Token* endTok = nextAfterAstRightmostLeaf(obj);
if (!endTok)
dotTok = tok2->next();
dotTok = obj->next();
else if (Token::simpleMatch(endTok, "."))
dotTok = endTok;
else if (Token::simpleMatch(endTok->next(), "."))
Expand All @@ -672,7 +679,6 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
if (Token::simpleMatch(dotTok, ".") && dotTok->originalName() == "->")
return true;
}
const Variable* var = tok2->variable();
return var && var->isReference();
}))
return nullptr;
Expand Down
7 changes: 7 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,13 @@ 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());

// #12600
check("struct S { std::unique_ptr<int> p; };\n"
"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

0 comments on commit 3135907

Please sign in to comment.