Skip to content

CPP: Add query for CWE-1126: Declaration of Variable with Unnecessarily Wide Scope#5767

Merged
geoffw0 merged 11 commits intogithub:mainfrom
ihsinme:ihsinme-patch-268
May 11, 2021
Merged

CPP: Add query for CWE-1126: Declaration of Variable with Unnecessarily Wide Scope#5767
geoffw0 merged 11 commits intogithub:mainfrom
ihsinme:ihsinme-patch-268

Conversation

@ihsinme
Copy link
Copy Markdown
Contributor

@ihsinme ihsinme commented Apr 25, 2021

Good day.
in this request I am looking at the error of using a new variable inside a loop.
in a situation where a variable with the same name participates in a loop condition.

the most dangerous situation is the while loop in the absence of changes to the variables participating in the condition.
At this stage, I tried to minimize false positives as much as possible.
I left the situation of changing a variable through a function call. as I considered it a rather rare practice. if you disagree with me, I will listen to you

current PR.
sleuthkit/sleuthkit#2329

@ihsinme ihsinme requested a review from a team as a code owner April 25, 2021 19:36
Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

This reminds me of the queries in Best Practices/Hiding, especially cpp/declaration-hides-variable. Your query appears to be a more specialized version looking for instances where there's evidence the hiding is causing a real problem.

@ihsinme
Copy link
Copy Markdown
Contributor Author

ihsinme commented May 2, 2021

thanks for your comments.
they are very helpful.

@geoffw0
Copy link
Copy Markdown
Contributor

geoffw0 commented May 6, 2021

Started a test run on LGTM: https://lgtm.com/query/3978555328191505856/

@ihsinme
Copy link
Copy Markdown
Contributor Author

ihsinme commented May 7, 2021

Started a test run on LGTM: https://lgtm.com/query/3978555328191505856/

good day.
I looked at the launch results.
I would like to point out the following
1.In the project ArtifexSoftware/ghostpdl, the detection on the change of a variable did not work, I need some time to figure it out.
2. libretro/libretro-uae WohlSoft/PGE-Project php/php-src projects showed false detection as I hastily replaced this.getCondition().GetAChild*(). this eliminated the control of changes for all variables in the loop condition. I will probably return the old version.

@ihsinme
Copy link
Copy Markdown
Contributor Author

ihsinme commented May 10, 2021

thanks for the note with recursion.
with its help, I managed to get rid of a number of logical negatives in the class constructor.
I also added two new loops to the test file.

@geoffw0
Copy link
Copy Markdown
Contributor

geoffw0 commented May 10, 2021

I've reported the comment two up, I think it might be some kind of spam. I suggest we ignore it and continue working on the query...

Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

I think you've addressed all of the suggestions, the QL is satisfactory and the tests are excellent.

Results on LGTM: https://lgtm.com/query/8106651676569760712/ (very few, probably because we're looking for cases that are definitely causing problems rather than just any instances of reusing variable names).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants