diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 00d593018..b262e4225 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -255,6 +255,11 @@ void CheckIO::checkFileUsage() } } } + for (std::map::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { + i->second.op_indent = 0; + i->second.mode = UNKNOWN; + i->second.lastOperation = Filepointer::UNKNOWN_OP; + } } } diff --git a/test/testio.cpp b/test/testio.cpp index cc487447c..c80bf15dd 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -287,6 +287,43 @@ private: " fclose(f[0]);\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #4368: multiple functions + check("static FILE *fp = NULL;\n" + "\n" + "void close()\n" + "{\n" + " fclose(fp);\n" + "}\n" + "\n" + "void dump()\n" + "{\n" + " if (fp == NULL) return;\n" + " fprintf(fp, \"Here's the output.\\n\");\n" + "}\n" + "\n" + "int main()\n" + "{\n" + " fp = fopen(\"test.txt\", \"w\");\n" + " dump();\n" + " close();\n" + " return 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("static FILE *fp = NULL;\n" + "\n" + "void close()\n" + "{\n" + " fclose(fp);\n" + "}\n" + "\n" + "void dump()\n" + "{\n" + " fclose(fp);\n" + " fprintf(fp, \"Here's the output.\\n\");\n" + "}"); + ASSERT_EQUALS("[test.cpp:11]: (error) Used file that is not opened.\n", errout.str()); } void fileIOwithoutPositioning() {