CheckMemoryLeak: Added simple checking

This commit is contained in:
Daniel Marjamäki 2008-08-16 16:47:54 +00:00
parent 8260cf459a
commit b29c916d9f
2 changed files with 60 additions and 9 deletions

View File

@ -562,6 +562,11 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
addtoken(tok->str);
}
// Return..
if ( Match(tok, "return %var1%", varnames) ||
Match(tok, "return & %var1%", varnames) )
addtoken("use");
// Assignment..
if ( Match(tok,"[)=] %var1%", varnames) )
addtoken("use");
@ -570,13 +575,24 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
if ( Match(tok, "[(,] %var1% [,)]", varnames) )
addtoken("use");
if ( Match( tok, "[=(,] & %var1% .", varnames ) )
// Linux lists..
if ( Match( tok, "[=(,] & %var1% [.[]", varnames ) )
addtoken("use");
}
return rethead;
}
static void eraseNext(TOKEN *tok)
{
if ( tok && tok->next )
{
TOKEN *next = tok->next;
tok->next = tok->next->next;
delete next;
}
}
// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All"
static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] )
@ -598,19 +614,38 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
// reduce the code..
for (TOKEN *tok2 = tok ; tok2; tok2 = tok2->next )
bool done = false;
while ( ! done )
{
while (Match(tok2,"[;{}] ;"))
done = true;
for (TOKEN *tok2 = tok ; tok2; tok2 = tok2->next )
{
TOKEN *next = tok2->next;
tok2->next = tok2->next->next;
delete next;
while (Match(tok2,"[;{}] ;"))
{
eraseNext(tok2);
done = false;
}
// Delete else { }
if ( Match(tok2->next, "else { }") )
{
eraseNext(tok2);
eraseNext(tok2);
eraseNext(tok2);
done = false;
}
}
}
if ( ! findmatch(tok,"dealloc") &&
! findmatch(tok,"return") &&
! findmatch(tok,"use") )
if ( findmatch(tok,"alloc ; return ;") )
{
MemoryLeak(gettok(findmatch(tok,"alloc ; return ;"),2), varname);
}
else if ( ! findmatch(tok,"dealloc") &&
! findmatch(tok,"use") &&
! findmatch(tok,"return use ;") )
{
const TOKEN *last = tok;
while (last->next)

View File

@ -510,6 +510,22 @@ static void memleak_in_function()
check_( CheckMemoryLeak, __LINE__, code, "" );
code = "static char *f()\n"
"{\n"
" char *s = new char[100];\n"
" return 0;\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "[test.cpp:4]: Memory leak: s\n" );
code = "static char *f()\n"
"{\n"
" struct *str = new strlist;\n"
" return &str->s;\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "" );
code = "static void f()\n"
"{\n"
" char *str = strdup(\"hello\");\n"