Skip to content

Fix #12618 FN variableScope with member or array access#6301

Merged
chrchr-github merged 5 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_Fix12618
Apr 18, 2024
Merged

Fix #12618 FN variableScope with member or array access#6301
chrchr-github merged 5 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_Fix12618

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread test/testother.cpp
check("struct S { int a; };\n" // #12618
"int f(const S* s, int i) {\n"
" int x = s->a;\n"
" const int b[] = { 1, 2, 3 };\n"

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.

Even the scope of 'b' could be reduced.

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.

yeah.. but that requires that scope of y can be reduced. I don't know if we will warn about b if y is moved.

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.

We don't warn for arrays because of

if (Token::Match(tok, "= %varid%", var->declarationId()) && (var->isArray() || var->isPointer() || (var->valueType() && var->valueType()->container))) // Create a copy of array/pointer. Bailout, because the memory it points to might be necessary in outer scope
            return false;

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 it's a bailout we should fix.. good that the comment says "bailout" explicitly so we can search for it..

@danmar danmar left a comment

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.

lgtm

@chrchr-github chrchr-github merged commit 5c97641 into cppcheck-opensource:main Apr 18, 2024
@chrchr-github chrchr-github deleted the chr_Fix12618 branch April 18, 2024 13:59
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