Skip to content

Commit 67edbe8

Browse files
fixup! fixup! fixup! fixup! fixup! fixup! Fix #958: warn when feof() is used as a while loop condition
1 parent f6a3573 commit 67edbe8

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

man/checkers/wrongfeofUsage.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99

1010
`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.
1111

12-
This checker warns when it finds feof in the loop condition and either:
13-
- no file-read call (e.g. `fgets`, `fread`, `fscanf`) precedes the loop and is also present inside the loop body
14-
- a control-flow statement (`return`, `break`, `goto`, `continue`, `throw`) is present in the the loop body.
12+
This checker matches `while (!feof(fp))` and `do { ... } while (!feof(fp))` loops and warns when all of the following are true:
13+
14+
- The loop body contains at least one file-read call (`fgets`, `fgetc`, `getc`, `fread`, or `fscanf`) on the same file pointer.
15+
- 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.
16+
17+
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`).
1518

1619
## How to fix
1720

0 commit comments

Comments
 (0)