Skip to content

Fix issue 9133: Invalid iterator; vector::push_back, functions#3008

Merged
danmar merged 9 commits into
cppcheck-opensource:mainfrom
pfultz2:invalid-container-ipa
Jan 11, 2021
Merged

Fix issue 9133: Invalid iterator; vector::push_back, functions#3008
danmar merged 9 commits into
cppcheck-opensource:mainfrom
pfultz2:invalid-container-ipa

Conversation

@pfultz2
Copy link
Copy Markdown
Contributor

@pfultz2 pfultz2 commented Jan 3, 2021

No description provided.

Copy link
Copy Markdown
Collaborator

@danmar danmar left a comment

Choose a reason for hiding this comment

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

some nits

Comment thread lib/astutils.cpp

int getArgumentPos(const Variable* var, const Function* f)
{
auto arg_it = std::find_if(f->argumentList.begin(), f->argumentList.end(), [&](const Variable& v) {
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 wonder if it would be a good idea to capture var by value not by reference.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why? It seems simpler and faster to capture by ref as it has lexical scoping and avoids creating copies.

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.

the motivation would be if we get some const safety. if it doesn't have a performance penalty.. that would be good. for a trivial example I made the assembler output from gcc is identical.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The variables are already declared const.

Copy link
Copy Markdown
Collaborator

@danmar danmar Jan 4, 2021

Choose a reason for hiding this comment

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

I assume that in the lambda you could write var=nullptr; and that will happily compile. well.. maybe that compiles happily with a = also?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yea, it can be modified with var=nullptr. I can make the pointer const.

Comment thread lib/checkstl.cpp Outdated
ErrorPath errorPath;
void add(const std::vector<Reference>& refs)
{
for (const Reference r : refs) {
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.

add a & please

Comment thread lib/checkstl.cpp
auto it = invalidMethods.find(f);
if (it != invalidMethods.end()) {
std::vector<Info::Reference> refs = it->second.invalidTokens();
std::copy_if(refs.begin(), refs.end(), std::back_inserter(result), [&](const Info::Reference& r) {
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 wonder if it would be a good idea to capture dependsOnThis by value. Does it actually capture everything in the context or are compilers smart enough and captures only dependsOnThis.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It only captures the variables used, but compilers can optimize it further and just capture the stack pointer instead of each variable used. Usually, its inlined so there is no copy(or copy to pointer) or function call(see this example).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's good practice to limit the amount of variable captured.

Comment thread lib/checkstl.cpp
return true;
});
std::vector<const Token*> args = getArguments(tok);
for (Info::Reference& r : result) {
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.

Is result nonempty here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No.

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.

so I guess you will fix something here before we merge it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What needs to be fixed?

Copy link
Copy Markdown
Collaborator

@danmar danmar Jan 7, 2021

Choose a reason for hiding this comment

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

I wonder .. if result is always empty. Is the loop redundant? If the loop is redundant then remove it.. but if it is not then please tell me how result could be nonempty..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Its not always empty. The copy_if fills the result.

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.

ok thanks for that clarification I somehow missed it.

Copy link
Copy Markdown
Collaborator

@danmar danmar left a comment

Choose a reason for hiding this comment

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

In my opinion this can be merged.. if the merge conflict is solved.

@pfultz2
Copy link
Copy Markdown
Contributor Author

pfultz2 commented Jan 11, 2021

Is there any more changes needed?

@danmar danmar merged commit b1c56d3 into cppcheck-opensource:main Jan 11, 2021
@pfultz2 pfultz2 deleted the invalid-container-ipa branch January 11, 2021 23:30
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.

3 participants