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
2 changes: 2 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,8 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
if (it == scope->varlist.cend())
return;
const Variable& var = *it;
if (var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think you need to check stdLikeString. A non-view container should always have ownership.

Looking at this closer it seems like this check should happen in the isLifetimeBorrowed function, but it probably doesn't get the parentValueType correctly.

@chrchr-github chrchr-github Jan 17, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

A string object will never own anything external*. As you noticed, isLifetimeBorrowed() doesn't even take a var argument.

*Unless it is a reference. But we already have a FN for that, because it doesn't involve a member function call and wouldn't hit the code path above.
https://trac.cppcheck.net/ticket/12407

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks very closely related to this FN:

https://trac.cppcheck.net/ticket/10885

I dont know if this fixes that issue or not.

Ideally we should check this in isLifetimeBorrowed function since this is a central place to check all the ownership/borrowing rules, but that looks to be a more complicated refactor.

This change is probably fine for now but probably should add a TODO comment(perhaps with a reference to this PR or discussion), so we don't end up duplicating a lot of logic here in the future.

Comment thread
chrchr-github marked this conversation as resolved.
Resolved
return; // TODO: check in isLifetimeBorrowed()?
if (var.isReference() || var.isRValueReference()) {
ls.byRef(tok, tokenlist, errorLogger, settings);
} else if (ValueFlow::isLifetimeBorrowed(ls.argtok, settings)) {
Expand Down
28 changes: 26 additions & 2 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TestAutoVariables : public TestFixture {
TEST_CASE(danglingLifetime);
TEST_CASE(danglingLifetimeFunction);
TEST_CASE(danglingLifetimeUserConstructor);
TEST_CASE(danglingLifetimeAggegrateConstructor);
TEST_CASE(danglingLifetimeAggregateConstructor);
TEST_CASE(danglingLifetimeInitList);
TEST_CASE(danglingLifetimeImplicitConversion);
TEST_CASE(danglingTemporaryLifetime);
Expand Down Expand Up @@ -3796,7 +3796,7 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void danglingLifetimeAggegrateConstructor() {
void danglingLifetimeAggregateConstructor() {
check("struct A {\n"
" const int& x;\n"
" int y;\n"
Expand Down Expand Up @@ -3893,6 +3893,30 @@ class TestAutoVariables : public TestFixture {
" return { m.data() };\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S { std::string s; };\n" // #13167
"std::vector<S> f() {\n"
" std::vector<S> v;\n"
" {\n"
" std::string a{ \"abc\" };\n"
" v.push_back({ a.c_str() });\n"
" }\n"
" return v;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
Comment thread
chrchr-github marked this conversation as resolved.
Resolved

check("struct S { std::string_view sv; };\n"
"std::vector<S> f() {\n"
" std::vector<S> v;\n"
" {\n"
" std::string a{ \"abc\" };\n"
" v.push_back({ a.c_str() });\n"
" }\n"
" return v;\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:6] -> [test.cpp:6] -> [test.cpp:6] -> [test.cpp:5] -> [test.cpp:8]: (error) Returning object that points to local variable 'a' that will be invalid when returning.\n",
errout_str());
}

void danglingLifetimeInitList() {
Expand Down