Skip to content
Merged
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
8 changes: 8 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3119,6 +3119,14 @@ static void valueFlowLifetime(TokenList &tokenlist, ErrorLogger &errorLogger, co
else if (tok->isUnaryOp("&")) {
if (Token::simpleMatch(tok->astParent(), "*"))
continue;
if (Token::simpleMatch(tok->astOperand1(), "[")) {
const Token* const op1 = tok->astOperand1()->astOperand1();
const Token* tok2 = op1;
while (Token::simpleMatch(tok2, "."))
tok2 = tok2->astOperand2();
if (tok2 && tok2 != op1 && (!tok2->variable() || !tok2->variable()->isArray()) && !(tok2->valueType() && tok2->valueType()->container))
continue;
}
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok->astOperand1(), settings)) {
if (!settings.certainty.isEnabled(Certainty::inconclusive) && lt.inconclusive)
continue;
Expand Down
21 changes: 21 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class TestAutoVariables : public TestFixture {
TEST_CASE(testautovar_return3);
TEST_CASE(testautovar_return4);
TEST_CASE(testautovar_return5);
TEST_CASE(testautovar_return6);
TEST_CASE(testautovar_extern);
TEST_CASE(testautovar_reassigned);
TEST_CASE(testinvaliddealloc);
Expand Down Expand Up @@ -624,6 +625,26 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void testautovar_return6() { // #14005
check("struct S;\n"
"struct T { const struct S *s; };\n"
"extern struct T factory();\n"
"const struct S* f() {\n"
" struct T t = factory();\n"
" return &t.s[0];\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S;\n"
"struct T { std::vector<struct S> v; };\n"
"extern struct T factory();\n"
"const struct S* f() {\n"
" struct T t = factory();\n"
" return &t.v[0];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:5:14] -> [test.cpp:6:12]: (error) Returning pointer to local variable 't' that will be invalid when returning. [returnDanglingLifetime]\n", errout_str());
}

void testautovar_extern() {
check("struct foo *f()\n"
"{\n"
Expand Down