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
16 changes: 14 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,19 @@ ValueFlow::Value ValueFlow::getLifetimeObjValue(const Token *tok, bool inconclus
return std::move(values.front());
}

static bool checkDotLifetime(const Token* dot, const Token* tok)
{
if (Token::simpleMatch(dot->astOperand1(), "."))
return false;
if (!dot->astOperand2())
return true;
if (dot->astOperand2()->varId() && tok->isUnaryOp("*"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This breaks for every non-array member with a varId under a unary *, including class-type members whose operator* returns a reference into the member itself. With struct S { std::optional<int> o; };, both int& f() { S s; return *s.o; } and int* g() { S s; return &*s.o; } lose the returnReference and returnDanglingLifetime that base reports. The same happens with a user struct W { int v; int& operator*() { return v; } }; member. Consider breaking only when the member's valueType is a real pointer (pointer > 0 and not an array) or a smart pointer or iterator, and following the lifetime otherwise.

return dot->astOperand2()->variable() && dot->astOperand2()->variable()->isArray();
if (dot->astOperand2()->valueType() && dot->astOperand2()->valueType()->reference != Reference::None)
return false;
return true;
}

template<class Predicate>
static std::vector<ValueFlow::LifetimeToken> getLifetimeTokens(const Token* tok,
bool escape,
Expand Down Expand Up @@ -1585,8 +1598,7 @@ static std::vector<ValueFlow::LifetimeToken> getLifetimeTokens(const Token* tok,
if (vartok->str() == "[" || vartok->isUnaryOp("*"))
vartok = vartok->astOperand1();
else if (vartok->str() == ".") {
if (!Token::simpleMatch(vartok->astOperand1(), ".") &&
!(vartok->astOperand2() && vartok->astOperand2()->valueType() && vartok->astOperand2()->valueType()->reference != Reference::None))
if (checkDotLifetime(vartok, tok))
vartok = vartok->astOperand1();
else
break;
Expand Down
17 changes: 17 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4581,6 +4581,23 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("[test.cpp:5:11] -> [test.cpp:4:9] -> [test.cpp:5:6]: (error) Non-local variable 'g.a' will use pointer to local variable 'x'. [danglingLifetime]\n"
"[test.cpp:5:11] -> [test.cpp:4:9] -> [test.cpp:6:13]: (error) Returning pointer to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
errout_str());

check("int* f() {\n" // #11168
" auto x = get();\n"
" return &*x.a;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S { int a[1]; };\n"
"int& f(S s) {\n"
" return *s.a;\n"
"}\n"
"int* g(S s) {\n"
" return &*s.a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:12]: (error) Reference to local variable returned. [returnReference]\n"
"[test.cpp:6:12] -> [test.cpp:5:10] -> [test.cpp:6:12]: (error) Returning pointer to local variable 's' that will be invalid when returning. [returnDanglingLifetime]\n",
errout_str());
}

void danglingLifetimeClassMemberFunctions()
Expand Down
Loading