Extended Message "fflushOnInputStream" to files opened for reading.

This commit is contained in:
PKEuS 2014-08-31 21:03:33 +02:00
parent b07f61170d
commit 288c94a47f
2 changed files with 29 additions and 4 deletions

View File

@ -176,12 +176,20 @@ void CheckIO::checkFileUsage()
operation = Filepointer::OPEN;
} else if ((tok->str() == "rewind" || tok->str() == "fseek" || tok->str() == "fsetpos" || tok->str() == "fflush") ||
(windows && tok->str() == "_fseeki64")) {
if (_settings->isEnabled("portability") && Token::simpleMatch(tok, "fflush ( stdin )"))
fflushOnInputStreamError(tok, tok->strAt(2));
if (_settings->isEnabled("portability") && tok->str() == "fflush") {
fileTok = tok->tokAt(2);
if (fileTok && fileTok->str() == "stdin")
fflushOnInputStreamError(tok, fileTok->str());
else {
Filepointer& f = filepointers[fileTok->varId()];
if (f.mode == READ_MODE)
fflushOnInputStreamError(tok, fileTok->str());
}
}
fileTok = tok->tokAt(2);
operation = Filepointer::POSITIONING;
}
} else if (tok->str() == "fgetc" || tok->str() == "fgetwc" ||
tok->str() == "fgets" || tok->str() == "fgetws" || tok->str() == "fread" ||
tok->str() == "fscanf" || tok->str() == "fwscanf" || tok->str() == "getc" ||

View File

@ -589,6 +589,23 @@ private:
" fflush(stdout);\n"
"}", false, true);
ASSERT_EQUALS("", errout.str());
check("void foo(FILE*& f) {\n"
" f = fopen(path, \"r\");\n"
" fflush(f);\n"
"}", false, true);
ASSERT_EQUALS("[test.cpp:3]: (portability) fflush() called on input stream 'f' may result in undefined behaviour on non-linux systems.\n", errout.str());
check("void foo(FILE*& f) {\n"
" f = fopen(path, \"w\");\n"
" fflush(f);\n"
"}", false, true);
ASSERT_EQUALS("", errout.str());
check("void foo(FILE*& f) {\n"
" fflush(f);\n"
"}", false, true);
ASSERT_EQUALS("", errout.str());
}