Repository navigation
Fix #5459 (Detect endless for loops) #9038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
willgarvin
wants to merge
8
commits into
cppcheck-opensource:main
Choose a base branch
from
willgarvin:Fix-#5459-(Detect-Endless-For-Loops)
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
985cbbe
Fix #5459 Detect Endless For Loops
willgarvin ea745c0
Merge pull request #1 from willgarvin/Fix-#5459-(Detect-Endless-For-L…
willgarvin ac9d650
Merge branch 'cppcheck-opensource:main' into main
willgarvin 644096a
Fix #5459 Detect Endless For Loops
willgarvin f562203
Merge branch 'cppcheck-opensource:main' into Fix-#5459-(Detect-Endles…
willgarvin ef2ff1a
Merge branch 'main' into Fix-#5459-(Detect-Endless-For-Loops)
willgarvin d4960e1
Fix CI: strip trailing whitespace and update active checker counts
willgarvin a7f0386
Updating PR based on review comments
willgarvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # missingForLoopIncrement | ||
|
|
||
| **Message**: The loop variable 'varname' is not updated in this for-loop, so the loop might never terminate.<br/> | ||
| **Category**: Correctness<br/> | ||
| **Severity**: Warning<br/> | ||
| **Language**: C, C++ | ||
|
|
||
| ## Description | ||
|
|
||
| A classic `for` loop's control variable is never advanced in the step clause or the loop body, so the | ||
| loop condition may never become false. | ||
|
|
||
| ## Motivation | ||
|
|
||
| Forgetting to increment an iterator or index is a common bug that leads to an infinite loop. Compilers | ||
| often do not warn about an empty for-loop step when the programmer intended to update the variable in | ||
| the body but never did. | ||
|
|
||
| ## How to fix | ||
|
|
||
| Before: | ||
| ```cpp | ||
| #include <map> | ||
| void f(std::map<int, int> &theMap) { | ||
| for (auto it = theMap.begin(); it != theMap.end();) { | ||
| (void)*it; // <- missingForLoopIncrement: 'it' is never updated | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| After: increment in the for-header or in the body. | ||
| ```cpp | ||
| #include <map> | ||
| void f(std::map<int, int> &theMap) { | ||
| for (auto it = theMap.begin(); it != theMap.end(); ++it) { | ||
| (void)*it; | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.