Skip to content

Commit 288c94a

Browse files
committed
Extended Message "fflushOnInputStream" to files opened for reading.
1 parent b07f611 commit 288c94a

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

lib/checkio.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,20 @@ void CheckIO::checkFileUsage()
176176
operation = Filepointer::OPEN;
177177
} else if ((tok->str() == "rewind" || tok->str() == "fseek" || tok->str() == "fsetpos" || tok->str() == "fflush") ||
178178
(windows && tok->str() == "_fseeki64")) {
179-
if (_settings->isEnabled("portability") && Token::simpleMatch(tok, "fflush ( stdin )"))
180-
fflushOnInputStreamError(tok, tok->strAt(2));
181-
else {
179+
if (_settings->isEnabled("portability") && tok->str() == "fflush") {
182180
fileTok = tok->tokAt(2);
183-
operation = Filepointer::POSITIONING;
181+
182+
if (fileTok && fileTok->str() == "stdin")
183+
fflushOnInputStreamError(tok, fileTok->str());
184+
else {
185+
Filepointer& f = filepointers[fileTok->varId()];
186+
if (f.mode == READ_MODE)
187+
fflushOnInputStreamError(tok, fileTok->str());
188+
}
184189
}
190+
191+
fileTok = tok->tokAt(2);
192+
operation = Filepointer::POSITIONING;
185193
} else if (tok->str() == "fgetc" || tok->str() == "fgetwc" ||
186194
tok->str() == "fgets" || tok->str() == "fgetws" || tok->str() == "fread" ||
187195
tok->str() == "fscanf" || tok->str() == "fwscanf" || tok->str() == "getc" ||

test/testio.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,23 @@ class TestIO : public TestFixture {
589589
" fflush(stdout);\n"
590590
"}", false, true);
591591
ASSERT_EQUALS("", errout.str());
592+
593+
check("void foo(FILE*& f) {\n"
594+
" f = fopen(path, \"r\");\n"
595+
" fflush(f);\n"
596+
"}", false, true);
597+
ASSERT_EQUALS("[test.cpp:3]: (portability) fflush() called on input stream 'f' may result in undefined behaviour on non-linux systems.\n", errout.str());
598+
599+
check("void foo(FILE*& f) {\n"
600+
" f = fopen(path, \"w\");\n"
601+
" fflush(f);\n"
602+
"}", false, true);
603+
ASSERT_EQUALS("", errout.str());
604+
605+
check("void foo(FILE*& f) {\n"
606+
" fflush(f);\n"
607+
"}", false, true);
608+
ASSERT_EQUALS("", errout.str());
592609
}
593610

594611

0 commit comments

Comments
 (0)