A loop tests feof before reading, so it runs once more after the last read fails

tx0/c:feof-looprecommendedwarningbug

What it finds

while (!feof(f)) { fgets(line, n, f); ... }: the end-of-file flag is set only after a read has failed, so the body runs once more with the last line again or with nothing read. Testing the result of the read itself ends the loop at the right time.

From the rule's source, feof_loop.rs.

Example from the rule's tests
#include <stdio.h> void lines(FILE *f, char *buf, int n){  while (!feof(f)) {tx0 reports: feof is true only after a read has failed    fgets(buf, n, f);  }  while (fgets(buf, n, f) != NULL)    puts(buf);}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:feof-loop" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/c:feof-loop] reason="..."

tx0 explain tx0/c:feof-loop shows the same in the terminal. Found a false positive? Report it.