CheckMemoryLeak: Small improvements

This commit is contained in:
Daniel Marjamäki 2008-08-19 04:53:41 +00:00
parent 9276783ab9
commit 0b8f646e0b
3 changed files with 55 additions and 18 deletions

View File

@ -511,7 +511,7 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
#define addtoken(_str) \
{ \
TOKEN *newtok = new TOKEN; \
newtok->str = _str; \
newtok->str = strdup(_str); \
newtok->linenr = tok->linenr; \
newtok->FileIndex = tok->FileIndex; \
newtok->next = 0; \
@ -657,7 +657,8 @@ static void erase(TOKEN *begin, const TOKEN *end)
while ( begin->next && begin->next != end )
{
TOKEN *next = begin->next;
begin->next = begin->next->next;
begin->next = begin->next->next;
free(next->str);
delete next;
}
}
@ -711,6 +712,12 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
erase( tok2->next->next, gettok(tok2,4) );
done = false;
}
if ( Match(tok2->next, "{ return use ; }") )
{
erase( tok2, gettok(tok2,2) );
erase( tok2->next->next->next, gettok(tok2,5) );
done = false;
}
// Delete empty if
if ( Match(tok2,"[;{}] if ;") ||
@ -736,6 +743,21 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
erase(tok2, gettok(tok2,3));
done = false;
}
// Delete if block: "alloc; if return use ;"
if (Match(tok2,"alloc ; if return use ;") && !Match(gettok(tok2,6),"else"))
{
erase(tok2, gettok(tok2,5));
done = false;
}
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
if (Match(tok2,"[;{}] if alloc ; else return ;"))
{
erase(tok2, gettok(tok2,2)); // Remove "if"
erase(tok2->next, gettok(tok2,5)); // Remove "; else return"
done = false;
}
// Delete "loop ;"
if ( Match(tok2->next, "loop ;") )
@ -758,7 +780,8 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
done = false;
}
}
}
}
if ( findmatch(tok, "alloc ; if continue ;") )
{

View File

@ -363,7 +363,8 @@ void deleteTokens(TOKEN *tok)
{
while (tok)
{
TOKEN *next = tok->next;
TOKEN *next = tok->next;
free(tok->str);
delete tok;
tok = next;
}

View File

@ -599,24 +599,22 @@ static void memleak_in_function()
check_( CheckMemoryLeak, __LINE__, code, "" );
/* TODO
code = "void f()\n"
"{\n"
" Fred *fred;\n"
" char *str;\n"
" if (somecondition)\n"
" {\n"
" fred = new Fred;\n"
" str = new char[100];\n"
" }\n"
" else\n"
" {\n"
" return;\n"
" }\n"
" delete fred;\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "" );
*/
" delete [] str;\n"
"}\n";
ShowAll = false;
check( CheckMemoryLeak, __LINE__, code, "" );
ShowAll = true;
code = "static char *f()\n"
"{\n"
@ -627,7 +625,7 @@ static void memleak_in_function()
" }\n"
" return NULL;\n"
"}\n";
check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Memory leak: s\n" );
check_( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Memory leak: s\n" );
code = "static char *f()\n"
@ -640,8 +638,8 @@ static void memleak_in_function()
" return s;\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "" );
/* todo
code = "static char *f()\n"
"{\n"
" char *s = new char[10];\n"
@ -650,9 +648,10 @@ static void memleak_in_function()
" return s;\n"
" }\n"
" return 0;\n"
"}\n";
"}\n";
ShowAll = false;
check( CheckMemoryLeak, __LINE__, code, "" );
*/
ShowAll = true;
@ -808,7 +807,21 @@ static void memleak_in_function()
*/
code = "static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)\n"
"{\n"
" struct fib6_table *table;\n"
"\n"
" table = kzalloc(sizeof(*table), GFP_ATOMIC);\n"
" if (table != NULL) {\n"
" table->tb6_id = id;\n"
" table->tb6_root.leaf = net->ipv6.ip6_null_entry;\n"
" table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;\n"
" }\n"
"\n"
" return table;\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "" );