Skip to content

Commit 5ef23b1

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

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

lib/checkio.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,7 @@ void CheckIO::getErrorMessages(ErrorLogger *errorLogger, const Settings *setting
20762076
c.useClosedFileError(nullptr);
20772077
c.seekOnAppendedFileError(nullptr);
20782078
c.incompatibleFileOpenError(nullptr, "tmp");
2079+
c.wrongfeofUsage(nullptr);
20792080
c.invalidScanfError(nullptr);
20802081
c.wrongPrintfScanfArgumentsError(nullptr, "printf",3,2);
20812082
c.invalidScanfArgTypeError_s(nullptr, 1, "s", nullptr);

test/testio.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,17 @@ class TestIO : public TestFixture {
755755
" fclose(fp);\n"
756756
"}");
757757
ASSERT_EQUALS("[test.cpp:3:10]: (warning) Using feof() as a loop condition may cause the last line to be processed twice. [wrongfeofUsage]\n", errout_str());
758+
759+
check("int foo() {\n"
760+
" FILE * fp = fopen(\"test.txt\", \"r\");\n"
761+
" char line[100];\n"
762+
" while (fgets(line, sizeof(line), fp)) {}\n"
763+
" if (!feof(fp))\n"
764+
" return 1;\n"
765+
" fclose(fp);\n"
766+
" return 0;\n"
767+
"}");
768+
ASSERT_EQUALS("", errout_str());
758769
}
759770

760771

0 commit comments

Comments
 (0)