memory leaks: Handling exit (#297)

This commit is contained in:
Daniel Marjamäki 2009-05-10 08:01:38 +02:00
parent f2a5527e60
commit daea0547fa
2 changed files with 52 additions and 0 deletions

View File

@ -662,6 +662,10 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
if (Token::Match(tok, "try|throw|catch"))
addtoken(tok->strAt(0));
// exit..
if (Token::Match(tok->previous(), "[{};] exit ( %any% ) ;"))
addtoken("exit");
// Assignment..
if (Token::Match(tok, std::string("[)=] " + varnameStr + " [+;)]").c_str()) ||
Token::Match(tok, std::string(varnameStr + " +=|-=").c_str()) ||
@ -771,6 +775,23 @@ void CheckMemoryLeakClass::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
const Token *tokEnd = tok2->next();
while (tok2->previous() && !Token::Match(tok2->previous(), "[{}]"))
tok2 = tok2->previous();
if (tok2->previous())
tok2 = tok2->previous();
Token::eraseTokens(tok2, tokEnd);
}
// reduce the code..
bool done = false;
while (! done)

View File

@ -208,6 +208,11 @@ private:
TEST_CASE(free_member_in_sub_func);
TEST_CASE(if_with_and);
TEST_CASE(assign_pclose);
// Using the function "exit"
TEST_CASE(exit1);
TEST_CASE(exit2);
}
@ -2121,6 +2126,32 @@ private:
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
void exit1()
{
// Ticket #297
check("void f()\n"
"{\n"
" char *out = new char[100];\n"
" if (c())\n"
" {\n"
" delete [] out;\n"
" exit(0);\n"
" }\n"
" delete [] out;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
void exit2()
{
check("void f()\n"
"{\n"
" char *out = new char[100];\n"
" exit(0);\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
};
REGISTER_TEST(TestMemleak)