Skip to content

Fix #1473: warn when fclose() is used as a while loop condition#8444

Open
francois-berder wants to merge 6 commits intocppcheck-opensource:mainfrom
francois-berder:pr-1473
Open

Fix #1473: warn when fclose() is used as a while loop condition#8444
francois-berder wants to merge 6 commits intocppcheck-opensource:mainfrom
francois-berder:pr-1473

Conversation

@francois-berder
Copy link
Copy Markdown
Collaborator

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.

@sonarqubecloud
Copy link
Copy Markdown

Comment thread test/testio.cpp Outdated
Comment thread lib/checkio.cpp Outdated
Comment thread lib/checkio.cpp Outdated
Comment thread lib/checkio.cpp Outdated
…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>
…close() is used as a while loop condition
Comment thread lib/checkio.cpp Outdated
Comment thread lib/checkio.cpp Outdated
Comment thread test/testio.cpp Outdated
… when fclose() is used as a while loop condition
Comment thread lib/checkio.cpp
{
reportError(tok, Severity::warning,
"fcloseInLoopCondition",
"fclose() should not be used as a loop condition.\n"
Copy link
Copy Markdown
Collaborator

@danmar danmar May 4, 2026

Choose a reason for hiding this comment

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

could there be some safe usage where fclose is used in the loop condition by intention:

  while (fclose(f)) {
    ..dostuff..
    f = fopen(..);
  }

it would be interesting if you would run test-my-pr.py and see if we generate any warnings..

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.

let's consider how to make the short message a bit more specific about what problems there are.

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.

2 participants