Fix #1473: warn when fclose() is used as a while loop condition#8444
Open
francois-berder wants to merge 3 commits intocppcheck-opensource:mainfrom
Open
Fix #1473: warn when fclose() is used as a while loop condition#8444francois-berder wants to merge 3 commits intocppcheck-opensource:mainfrom
francois-berder wants to merge 3 commits intocppcheck-opensource:mainfrom
Conversation
…oop condition Using fclose() as a while loop condition closes the file on every iteration and operates on an already-closed file handle from the second iteration onward. Signed-off-by: Francois Berder <fberder@outlook.fr>
|
danmar
reviewed
Apr 25, 2026
…while loop condition
…d as a while loop condition
danmar
reviewed
Apr 28, 2026
| const Token* bodyEnd = nullptr; | ||
| const Token* bodyStart = nullptr; | ||
|
|
||
| if (Token::simpleMatch(loopTok->previous(), "}")) { // Handle do-while loops |
Collaborator
There was a problem hiding this comment.
it doesn't have to be a do-while when previous token is a "}".
| operation = Filepointer::Operation::CLOSE; | ||
|
|
||
| // #1473 Check if fclose is in a while loop condition | ||
| if (fileTok) { |
Collaborator
There was a problem hiding this comment.
the old code does not use AST so it doesn't handle well when the parameter is some expression.
imagine:
while (i < 10 && fclose(f[i++]) {}
It is ugly code. I suggest you check that fileTok is just a simple variable token.
| } | ||
|
|
||
| // Do not trigger a warning if the loop always exits or if the file is opened again in the loop. | ||
| if (!isReturnScope(bodyEnd, mSettings->library) && Token::findmatch(bodyStart, "%var% = fopen|freopen", bodyEnd, fileTok->varId()) == nullptr) |
Collaborator
There was a problem hiding this comment.
if there is any f = some_function(); in the loop we shouldn't warn. I suggest:
Suggested change
| if (!isReturnScope(bodyEnd, mSettings->library) && Token::findmatch(bodyStart, "%var% = fopen|freopen", bodyEnd, fileTok->varId()) == nullptr) | |
| if (!isReturnScope(bodyEnd, mSettings->library) && Token::findmatch(bodyStart, "%var% =", bodyEnd, fileTok->varId()) == nullptr) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Using fclose() as a while loop condition closes the file on every iteration and operates on an already-closed file handle from the second iteration onward.