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.

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.

I spent a bit of time looking for the right message. In the end, I concluded that we should nearly always warn if fclose is used in a while loop condition simply because it is a code smell. This is why, I also set the severity of the message to warning to better reflect the fact that it may not always cause issues.

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.

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

Good point, I will try that but it takes a little while to run. By the way, I spent a bit of time improving the various scripts (daca2-download.py, daca2-getpackages.py and test-my-pr.py) but it is not yet ready.

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