Fix #1473: warn when fclose() is used as a while loop condition#8444
Fix #1473: warn when fclose() is used as a while loop condition#8444francois-berder wants to merge 6 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>
…while loop condition
…d as a while loop condition
… is used as a while loop condition
…close() is used as a while loop condition
3491db9 to
28f2247
Compare
… when fclose() is used as a while loop condition
| { | ||
| reportError(tok, Severity::warning, | ||
| "fcloseInLoopCondition", | ||
| "fclose() should not be used as a loop condition.\n" |
There was a problem hiding this comment.
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..
There was a problem hiding this comment.
let's consider how to make the short message a bit more specific about what problems there are.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.



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.