Skip to content

Fix #10704 FN redundantCopyLocalConst#4115

Merged
danmar merged 18 commits intocppcheck-opensource:mainfrom
chrchr-github:chr_Fix10704
Jul 10, 2022
Merged

Fix #10704 FN redundantCopyLocalConst#4115
danmar merged 18 commits intocppcheck-opensource:mainfrom
chrchr-github:chr_Fix10704

Conversation

@chrchr-github
Copy link
Copy Markdown
Collaborator

No description provided.

@chrchr-github chrchr-github marked this pull request as draft May 18, 2022 17:42
Comment thread lib/tokenize.cpp
continue;

std::string name = tok->strAt(1);
const std::string& name = tok->strAt(1);
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 and most (all?) of the other changes seem a bit dangerous. We need to be sure that the owner is const and/or only const member functions are being called. But this holds only true for the moment. If the code is changed afterwards that would actually issues.

It is true that the code could have been written like this in the first place but that was either intentional or erroneous by the coder and not suggested by a tool perceived as "trustworthy".

I did such overzealous changes in #4034 which lead to crashes in the tests.

The example in the ticket which inspired this is valid but there is only a single usage and the call there could be inlined in the single usage and not variable is even necessary.

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.

I agree that this warning can be dangerous, see the suppression I had to add. On the other hand, the check would have already fired had the local variables been declared as const. That is just a heuristic though and does not indicate whether using a reference is actually safe or not.

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.

On the other hand, the check would have already fired had the local variables been declared as const.

I knew there was an existing case where we suggest this. I just couldn't remember. So I guess this is fine.

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 agree that this feels a bit dangerous. Tokens are deleted in this function.
Are you sure there is no path where the tok will be deleted before name is accessed?

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.

name is only read at L2367, deletions start at L2386. So this should be safe.

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.

But would cppcheck detect the issue if the code was changed in the future to read name after the token was deleted?

At least using a copy this will be more resilient to changes in the future.

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.

No, we would not detect that, see the suppression that I added elsewhere. I don't think the checker has many clues here to decide if it should warn or not.

@chrchr-github
Copy link
Copy Markdown
Collaborator Author

Blocked by https://trac.cppcheck.net/ticket/11083 for the moment.

@chrchr-github chrchr-github marked this pull request as ready for review May 31, 2022 14:55
Comment thread test/testother.cpp
Comment thread lib/astutils.cpp Outdated
Comment thread lib/checkother.cpp
Comment thread lib/astutils.cpp Outdated
Comment thread lib/tokenize.cpp
continue;

std::string name = tok->strAt(1);
const std::string& name = tok->strAt(1);
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 agree that this feels a bit dangerous. Tokens are deleted in this function.
Are you sure there is no path where the tok will be deleted before name is accessed?

Comment thread lib/astutils.cpp Outdated
Comment thread lib/astutils.cpp Outdated
Comment thread lib/astutils.cpp Outdated
@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Jun 19, 2022

It seems the checker needs to be more robust first. The suppression shows a dangerous false positive that could cause bugs.

@chrchr-github
Copy link
Copy Markdown
Collaborator Author

Hmm, there are only three true positive test cases so far, and they all concern static (member) variables. I agree that we cannot warn if the underlying resource might be modified in any way.

@chrchr-github
Copy link
Copy Markdown
Collaborator Author

Any more feedback?

@danmar danmar merged commit f5c4a21 into cppcheck-opensource:main Jul 10, 2022
@chrchr-github chrchr-github deleted the chr_Fix10704 branch October 22, 2022 09:49
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.

4 participants