Skip to content

Fix #13167 FP invalidLifetime when constructing string from c_str() - #7231

Merged
chrchr-github merged 4 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_13167
Jan 18, 2025
Merged

chrchr-github merged 4 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_13167

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread test/testautovariables.cpp Resolved
Comment thread lib/valueflow.cpp
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 lib/valueflow.cpp Resolved
@chrchr-github
chrchr-github merged commit 204c821 into cppcheck-opensource:main Jan 18, 2025
@chrchr-github
chrchr-github deleted the chr_13167 branch January 18, 2025 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants