Fixed #483 (False positive: found leak when foreach for lists used)

This commit is contained in:
Daniel Marjamäki 2009-11-15 10:30:00 +01:00
parent 14fd0154d2
commit b5cbc509f3
2 changed files with 22 additions and 1 deletions

View File

@ -1081,10 +1081,22 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
// Investigate function calls..
if (Token::Match(tok, "%var% ("))
{
// A function call should normally be followed by ";"
if (Token::simpleMatch(tok->next()->link(), ") {"))
{
if (!Token::Match(tok, "if|for|while|switch"))
{
addtoken("exit");
addtoken(";");
tok = tok->next()->link();
continue;
}
}
// Inside class function.. if the var is passed as a parameter then
// just add a "::use"
// The "::use" means that a member function was probably called but it wasn't analyzed further
if (classmember)
else if (classmember)
{
int parlevel = 1;
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
@ -1746,6 +1758,11 @@ const Token *CheckMemoryLeakInFunction::findleak(const Token *tokens, bool all)
return result;
}
if (Token::Match(tokens, "alloc ; if|if(var)|ifv break|continue|return ;"))
{
return tokens->tokAt(3);
}
if ((result = Token::findmatch(tokens, "alloc ; if|if(var)|ifv return ;")) != NULL)
{
return result->tokAt(3);

View File

@ -403,6 +403,9 @@ private:
ASSERT_EQUALS(";;exit;", getcode("char *s; err(0);", "s"));
ASSERT_EQUALS(";;if{exit;}", getcode("char *s; if (a) { exit(0); }", "s"));
// list_for_each
ASSERT_EQUALS(";;exit;{}", getcode("char *s; list_for_each(x,y,z) { }", "s"));
// open/close
ASSERT_EQUALS(";;alloc;if(var){dealloc;}", getcode("int f; f=open(); if(f>=0)close(f);", "f"));
ASSERT_EQUALS(";;alloc;ifv{;}", getcode("int f; f=open(); if(f!=-1 || x);", "f"));
@ -576,6 +579,7 @@ private:
// if..
ASSERT_EQUALS(-1, dofindleak("alloc; ifv dealloc;"));
ASSERT_EQUALS(2, dofindleak("alloc;\n if return;\n dealloc;"));
ASSERT_EQUALS(2, dofindleak("alloc;\n if continue;\n dealloc;"));
ASSERT_EQUALS(2, dofindleak("alloc;\n if_var return;\n dealloc;"));
ASSERT_EQUALS(3, dofindleak("alloc;\n if\n return;\n dealloc;"));
ASSERT_EQUALS(-1, dofindleak("alloc; if { dealloc ; return; } dealloc;"));