Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/checkio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ void CheckIO::checkFileUsage()
} else if (tok->str() == "fclose") {
fileTok = tok->tokAt(2);
operation = Filepointer::Operation::CLOSE;

// #1473 Check if fclose is in a while loop condition
const Token* tmp = tok->astParent();
const Token* loopTok = nullptr;
while (tmp) {
if (Token::simpleMatch(tmp->previous(), "while (")) {
loopTok = tmp->previous();
break;
}
tmp = tmp->astParent();
}
if (loopTok)
useClosedFileError(tok);
} else if (whitelist.find(tok->str()) != whitelist.end()) {
fileTok = tok->tokAt(2);
if ((tok->str() == "ungetc" || tok->str() == "ungetwc") && fileTok)
Expand Down
2 changes: 1 addition & 1 deletion test/testio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ class TestIO : public TestFixture {
" FILE *a = fopen(\"aa\", \"r\");\n"
" while (fclose(a)) {}\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:3:5]: (error) Used file that is not opened. [useClosedFile]\n", "", errout_str());
ASSERT_EQUALS("[test.cpp:3:12]: (error) Used file that is not opened. [useClosedFile]\n", errout_str());

// #6823
check("void foo() {\n"
Expand Down
Loading