Skip to content

Commit 4aa6ce6

Browse files
fixup! fixup! fixup! Fix #958: warn when feof() is used as a while loop condition
1 parent 6161e99 commit 4aa6ce6

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
@@ -506,9 +506,14 @@ void CheckIO::checkWrongfeofUsage()
506506

507507
for (const Scope * scope : symbolDatabase->functionScopes) {
508508
for (const Token *tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
509+
// TODO: Handle do-while and for loops
509510
if (!Token::simpleMatch(tok, "while ( ! feof ("))
510511
continue;
511512

513+
// Bail out if we reach a do-while loop
514+
if (Token::simpleMatch(tok->previous(), "}") && Token::simpleMatch(tok->linkAt(-1)->previous(), "do"))
515+
continue;
516+
512517
// Bail out if we cannot identify file pointer
513518
const int fpVarId = tok->tokAt(5)->varId();
514519
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)