From e2655da1ec82341e39c266f59ee07f8013a9f823 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 15 Feb 2013 08:30:43 -0800 Subject: [PATCH] Properly handle return/break/continue in CheckIO::checkFileUsage() (#4466) --- lib/checkio.cpp | 9 ++++++++- test/testio.cpp | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index fbf84bfc1..2fa2a094a 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -140,6 +140,13 @@ void CheckIO::checkFileUsage() i->second.lastOperation = Filepointer::UNKNOWN_OP; } } + } else if (tok->str() == "return" || tok->str() == "continue" || tok->str() == "break") { // Reset upon return, continue or break + for (std::map::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { + i->second.mode_indent = 0; + i->second.mode = UNKNOWN; + i->second.op_indent = 0; + i->second.lastOperation = Filepointer::UNKNOWN_OP; + } } else if (tok->varId() && Token::Match(tok, "%var% =") && (tok->strAt(2) != "fopen" && tok->strAt(2) != "freopen" && tok->strAt(2) != "tmpfile")) { std::map::iterator i = filepointers.find(tok->varId()); if (i != filepointers.end()) { @@ -187,7 +194,7 @@ void CheckIO::checkFileUsage() if (tok->str() == "ungetc" && fileTok) fileTok = fileTok->nextArgument(); operation = Filepointer::UNIMPORTANT; - } else if (!Token::Match(tok, "if|for|while|catch|return")) { + } else if (!Token::Match(tok, "if|for|while|catch|switch")) { const Token* const end2 = tok->linkAt(1); for (const Token* tok2 = tok->tokAt(2); tok2 != end2; tok2 = tok2->next()) { if (tok2->varId() && filepointers.find(tok2->varId()) != filepointers.end()) { diff --git a/test/testio.cpp b/test/testio.cpp index 2f00f236f..4f387d842 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -324,6 +324,19 @@ private: " fprintf(fp, \"Here's the output.\\n\");\n" "}"); ASSERT_EQUALS("[test.cpp:11]: (error) Used file that is not opened.\n", errout.str()); + + // #4466 + check("void chdcd_parse_nero(FILE *infile) {\n" + " switch (mode) {\n" + " case 0x0300:\n" + " fclose(infile);\n" + " return;\n" + " case 0x0500:\n" + " fclose(infile);\n" + " return;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void fileIOwithoutPositioning() {