You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix#958: warn when feof() is used as a while loop condition
feof() only returns true after a read has already failed, causing
the loop body to execute once more after the last successful read.
Read errors also go undetected since feof() does not distinguish
them from EOF.
Signed-off-by: Francois Berder <fberder@outlook.fr>
Copy file name to clipboardExpand all lines: test/testio.cpp
+31Lines changed: 31 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,7 @@ class TestIO : public TestFixture {
45
45
TEST_CASE(seekOnAppendedFile);
46
46
TEST_CASE(fflushOnInputStream);
47
47
TEST_CASE(incompatibleFileOpen);
48
+
TEST_CASE(testWrongfeofUsage); // #958
48
49
49
50
TEST_CASE(testScanf1); // Scanf without field limiters
50
51
TEST_CASE(testScanf2);
@@ -743,6 +744,36 @@ class TestIO : public TestFixture {
743
744
ASSERT_EQUALS("[test.cpp:3:16]: (warning) The file '\"tmp\"' is opened for read and write access at the same time on different streams [incompatibleFileOpen]\n", errout_str());
744
745
}
745
746
747
+
voidtestWrongfeofUsage() { // ticket #958
748
+
check("void foo(FILE * fp) {\n"
749
+
" while (!feof(fp)) \n"
750
+
" {\n"
751
+
" char line[100];\n"
752
+
" fgets(line, sizeof(line), fp);\n"
753
+
" }\n"
754
+
"}");
755
+
ASSERT_EQUALS("[test.cpp:2:10]: (warning) Using feof() as a loop condition causes the last line to be processed twice. [wrongfeofUsage]\n", errout_str());
0 commit comments