Skip to content

Commit e835fca

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

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

lib/checkio.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,14 @@ void CheckIO::checkWrongfeofUsage()
532532

533533
for (const Scope * scope : symbolDatabase->functionScopes) {
534534
for (const Token *tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
535+
// TODO: Handle do-while and for loops
535536
if (!Token::simpleMatch(tok, "while ( ! feof ("))
536537
continue;
537538

539+
// Bail out if we reach a do-while loop
540+
if (Token::simpleMatch(tok->previous(), "}") && Token::simpleMatch(tok->linkAt(-1)->previous(), "do"))
541+
continue;
542+
538543
// Bail out if we cannot identify file pointer
539544
const int fpVarId = tok->tokAt(5)->varId();
540545
if (fpVarId == 0)

man/checkers/wrongfeofUsage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## Description
99

10-
`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 `while` 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.
10+
`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

1212
This checker warns when it finds feof in the loop condition and either:
1313
- no file-read call (e.g. `fgets`, `fread`, `fscanf`) precedes the loop and is also present inside the loop body

0 commit comments

Comments
 (0)