memory leak: improved the checking. subfunctions are parsed. redundant conditions are skipped
This commit is contained in:
parent
81bcde9ed0
commit
ff6adcbeff
|
@ -83,6 +83,14 @@ static AllocType GetAllocationType( const TOKEN *tok2 )
|
||||||
|
|
||||||
static AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[] )
|
static AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[] )
|
||||||
{
|
{
|
||||||
|
// Redundant condition..
|
||||||
|
if ( Match(tok, "if ( %var1% )", varnames) )
|
||||||
|
{
|
||||||
|
tok = gettok( tok, 4 );
|
||||||
|
if ( Match(tok,"{") )
|
||||||
|
tok = tok->next;
|
||||||
|
}
|
||||||
|
|
||||||
if ( Match(tok, "delete %var1% ;", varnames) )
|
if ( Match(tok, "delete %var1% ;", varnames) )
|
||||||
return New;
|
return New;
|
||||||
|
|
||||||
|
@ -156,8 +164,84 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
||||||
dealloc_indentlevel = -1;
|
dealloc_indentlevel = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check subfunction...
|
||||||
|
if (Alloc != No && Match(tok,"[{};] %var% ("))
|
||||||
|
{
|
||||||
|
const char *funcname = getstr( tok, 1 );
|
||||||
|
|
||||||
|
unsigned int param = 0;
|
||||||
|
for (const TOKEN *tok2 = gettok(tok,2); tok2; tok2 = tok2->next)
|
||||||
|
{
|
||||||
|
if ( tok2->str[0] == ';' )
|
||||||
|
break;
|
||||||
|
if ( tok2->str[0] == ',' )
|
||||||
|
param++;
|
||||||
|
|
||||||
|
if ( Match(tok2, "[(,] %var1% [,)]", varnames) )
|
||||||
|
{
|
||||||
|
// Find function..
|
||||||
|
const TOKEN *ftok = GetFunctionTokenByName( funcname );
|
||||||
|
ftok = gettok(ftok,2);
|
||||||
|
if ( ! ftok )
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Goto function parameter..
|
||||||
|
for ( unsigned int fparam = 0; ftok && fparam < param; ftok = ftok->next )
|
||||||
|
{
|
||||||
|
if ( ftok->str[0] == ',' )
|
||||||
|
++fparam;
|
||||||
|
}
|
||||||
|
for ( ; ftok; ftok = ftok->next )
|
||||||
|
{
|
||||||
|
if ( ! Match(ftok,"%var% [,)]") )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const char *paramname[2] = {0};
|
||||||
|
paramname[0] = ftok->str;
|
||||||
|
// parse function and check if it deallocates the parameter..
|
||||||
|
int _indentlevel = 0;
|
||||||
|
while (_indentlevel>=0 && ftok)
|
||||||
|
{
|
||||||
|
if ( ftok->str[0] == '{' )
|
||||||
|
_indentlevel++;
|
||||||
|
else if ( ftok->str[0] == '}' )
|
||||||
|
{
|
||||||
|
_indentlevel--;
|
||||||
|
if ( _indentlevel <= 0 )
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( _indentlevel >= 1 )
|
||||||
|
{
|
||||||
|
AllocType dealloc = GetDeallocationType(ftok,paramname);
|
||||||
|
if ( dealloc != No )
|
||||||
|
{
|
||||||
|
if ( Alloc != No && Alloc != dealloc )
|
||||||
|
{
|
||||||
|
MismatchError( Tok1, varname );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deallocated at same indentlevel as the allocation => no memory leak
|
||||||
|
if ( alloc_indentlevel == indentlevel )
|
||||||
|
return;
|
||||||
|
|
||||||
|
dealloc_indentlevel = indentlevel;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ftok = ftok->next;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// for, while set loop level..
|
// for, while set loop level..
|
||||||
if ( alloc_indentlevel >= 0 && (Match(tok,"while") || Match(tok,"for")) )
|
if ( Match(tok,"while") || Match(tok,"for") )
|
||||||
loop_indentlevel.push_back( indentlevel );
|
loop_indentlevel.push_back( indentlevel );
|
||||||
|
|
||||||
// Skip stuff like: if (!var) ...
|
// Skip stuff like: if (!var) ...
|
||||||
|
@ -247,8 +331,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
||||||
// Used..
|
// Used..
|
||||||
// list.push_back( var1 );
|
// list.push_back( var1 );
|
||||||
// listtail->next = var1;
|
// listtail->next = var1;
|
||||||
// foo( var1 );
|
if ( Match( tok, "[=] %var1% [;]", varnames ) )
|
||||||
if ( Match( tok, "[=,(] %var1% [,);]", varnames ) )
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
26
tests.cpp
26
tests.cpp
|
@ -521,7 +521,7 @@ static void memleak_in_function()
|
||||||
" int *a = new int[10];\n"
|
" int *a = new int[10];\n"
|
||||||
" if (a)\n"
|
" if (a)\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
" delete a;\n"
|
" delete [] a;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
check( CheckMemoryLeak, __LINE__, code, "" );
|
check( CheckMemoryLeak, __LINE__, code, "" );
|
||||||
|
@ -619,18 +619,6 @@ static void memleak_in_function()
|
||||||
check( CheckMemoryLeak, __LINE__, code, "" );
|
check( CheckMemoryLeak, __LINE__, code, "" );
|
||||||
|
|
||||||
|
|
||||||
/* TODO
|
|
||||||
code = "void f()\n"
|
|
||||||
"{\n"
|
|
||||||
" char *str;\n"
|
|
||||||
" for (int i = 0; i < 10; ++i)\n"
|
|
||||||
" str = strdup(\"hello\");\n"
|
|
||||||
" free(str);\n"
|
|
||||||
"}\n";
|
|
||||||
check( CheckMemoryLeak, __LINE__, code, "[test.cpp:5]: Memory leak: str" );
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
code = "void f()\n"
|
code = "void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" for (int i = 0; i < j; i++)\n"
|
" for (int i = 0; i < j; i++)\n"
|
||||||
|
@ -777,6 +765,18 @@ static void memleak_in_function()
|
||||||
" delete p;\n"
|
" delete p;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Mismatching allocation and deallocation: p\n" );
|
check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Mismatching allocation and deallocation: p\n" );
|
||||||
|
|
||||||
|
|
||||||
|
code = "static void foo(const char *str)\n"
|
||||||
|
"{ }\n"
|
||||||
|
"\n"
|
||||||
|
"static void f()\n"
|
||||||
|
"{\n"
|
||||||
|
" char *p = new char[100];\n"
|
||||||
|
" foo(p);\n"
|
||||||
|
"}\n";
|
||||||
|
check( CheckMemoryLeak, __LINE__, code, "[test.cpp:6]: Memory leak: p\n" );
|
||||||
|
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue