diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index cf47b59dc..7245e4243 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1795,6 +1795,20 @@ const Token *CheckMemoryLeakInFunction::findleak(const Token *tokens, bool all) const Token *last = tokens; while (last->next()) last = last->next(); + + // check if we call exit before the end of the funcion + Token *tok2 = last->previous(); + if (tok2) + { + if (Token::simpleMatch(tok2, ";")) { + const Token *tok3 = tok2->previous(); + if (tok3 && Token::simpleMatch(tok3, "exit")) + { + return NULL; + } + } + } + return last; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index c1f941536..dd87a4c73 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2078,6 +2078,25 @@ private: ASSERT_EQUALS("", errout.str()); } + void exit6() + { + check("int main(int argc, char *argv[]) {\n" + " FILE *sfile;\n" + " unsigned long line;\n" + " sfile = fopen(\"bar\", \"r\");\n" + " if (!sfile)\n" + " return 1;\n" + " for(line = 0; ; line++) {\n" + " if (argc > 3)\n" + " break;\n" + " exit(0);\n" + " }\n" + " fclose(sfile);\n" + " exit(0);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void noreturn() { check("void fatal_error()\n"