From 565ac2fca665ff30dd142b77781d78bb8a96875e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 23 Jul 2009 16:30:30 +0200 Subject: [PATCH] Fixed #493 (Memory leak: False positive when using the exit(0)) --- src/checkmemoryleak.cpp | 56 +++++++++++++++++++---------------------- test/testmemleak.cpp | 30 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 7b962f917..55912ad08 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -895,7 +895,10 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listprevious(), "[{};] exit ( %any% ) ;")) + { addtoken("exit"); + tok = tok->tokAt(3); + } // Assignment.. if (Token::Match(tok, std::string("[)=] " + varnameStr + " [+;)]").c_str()) || @@ -1006,36 +1009,6 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) tok2->str("return"); } - - // simplify "exit".. remove everything in its execution path - for (Token *tok2 = tok; tok2; tok2 = tok2->next()) - { - if (tok2->str() != "exit") - continue; - - // Found an "exit".. try to remove everything before it - Token *tokEnd = tok2->next(); - int indentlevel = 0; - while (tok2->previous()) - { - tok2 = tok2->previous(); - if (tok2->str() == "}") - { - indentlevel--; - } - else if (tok2->str() == "{") - { - if (indentlevel == 0) - break; - - indentlevel++; - } - } - - Token::eraseTokens(tok2, tokEnd); - tok2 = tokEnd; - } - // If "--all" is given, remove all "callfunc".. if (_settings->_showAll) { @@ -1257,6 +1230,27 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) done = false; } + // Reduce "alloc|dealloc|use ; exit ;" => "; exit ;" + if (Token::Match(tok2, "alloc|dealloc|use ; exit ;")) + { + tok2->deleteThis(); + done = false; + } + + // Reduce "alloc|dealloc|use ; if(var) exit ;" + if (Token::Match(tok2, "alloc|dealloc|use ; if(var) exit ;")) + { + tok2->deleteThis(); + done = false; + } + + // Remove "if exit ;" + if (Token::simpleMatch(tok2, "if exit ;")) + { + tok2->deleteThis(); + tok2->deleteThis(); + done = false; + } // Replace "dealloc use ;" with "dealloc ;" if (Token::simpleMatch(tok2, "dealloc use ;")) @@ -1485,6 +1479,8 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) } } } + + } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index f8f0f9299..48ef0079a 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -265,6 +265,8 @@ private: // Using the function "exit" TEST_CASE(exit1); TEST_CASE(exit2); + TEST_CASE(exit3); + TEST_CASE(exit4); TEST_CASE(stdstring); TEST_CASE(strndup_function); @@ -2118,6 +2120,34 @@ private: ASSERT_EQUALS("", errout.str()); } + void exit3() + { + check("void f()\n" + "{\n" + " char *p = malloc(100);\n" + " if (p)\n" + " {\n" + " exit(0);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + + void exit4() + { + check("void f()\n" + "{\n" + " char *p = malloc(100);\n" + " if (x)\n" + " {\n" + " exit(0);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p\n", errout.str()); + } + + + void stdstring() { check("void f(std::string foo)\n"