-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix #958: warn when feof() is used as a while loop condition #8422
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
francois-berder
wants to merge
7
commits into
cppcheck-opensource:main
Choose a base branch
from
francois-berder:pr-958
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.
+235
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9cdb531
Fix #958: warn when feof() is used as a while loop condition
francois-berder 2ab0ce4
fixup! Fix #958: warn when feof() is used as a while loop condition
francois-berder 0f08696
fixup! fixup! Fix #958: warn when feof() is used as a while loop cond…
francois-berder 6fd1296
fixup! fixup! fixup! Fix #958: warn when feof() is used as a while lo…
francois-berder 43d0b58
fixup! fixup! fixup! fixup! Fix #958: warn when feof() is used as a w…
francois-berder f6a3573
fixup! fixup! fixup! fixup! fixup! Fix #958: warn when feof() is used…
francois-berder 67edbe8
fixup! fixup! fixup! fixup! fixup! fixup! Fix #958: warn when feof() …
francois-berder 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,42 @@ | ||
| # wrongfeofUsage | ||
|
|
||
| **Message**: Using feof() as a loop condition causes the last line to be processed twice.<br/> | ||
| **Category**: Correctness<br/> | ||
| **Severity**: Warning<br/> | ||
| **Language**: C/C++ | ||
|
|
||
| ## Description | ||
|
|
||
| `feof()` returns non-zero only after a read operation has failed because the end of file was reached. When used as the sole condition of a loop, the loop body executes one extra time after the last successful read: the read fails silently (or returns partial data), and only then does `feof()` return true and terminate the loop. | ||
|
|
||
| This checker matches `while (!feof(fp))` and `do { ... } while (!feof(fp))` loops and warns when all of the following are true: | ||
|
|
||
| - The loop body contains at least one file-read call (`fgets`, `fgetc`, `getc`, `fread`, or `fscanf`) on the same file pointer. | ||
| - The destination (return value or output buffer) of the **last** file-read call in the loop is used after that call within the same loop iteration. | ||
|
|
||
| The checker skips loops that contain a control-flow statement (`return`, `break`, `goto`, `continue`, `throw`) as those are too complex to analyze reliably, and loops where the file pointer appears in a context other than a recognised I/O function (`fgets`, `fgetc`, `getc`, `fread`, `fscanf`, `fprintf`, `fwrite`, `fputs`, `fputc`, `putc`). | ||
|
|
||
| ## How to fix | ||
|
|
||
| Check the return value of the read function directly in the loop condition. | ||
|
|
||
| Before: | ||
| ```c | ||
| void process(FILE *fp) { | ||
| char line[256]; | ||
| while (!feof(fp)) { /* wrong: processes last line twice */ | ||
| fgets(line, sizeof(line), fp); | ||
| puts(line); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| After: | ||
| ```c | ||
| void process(FILE *fp) { | ||
| char line[256]; | ||
| while (fgets(line, sizeof(line), fp) != NULL) { | ||
| puts(line); | ||
| } | ||
| } | ||
| ``` |
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
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.