CheckMemoryLeak: Improved the checking
(tests.cpp:memleak_in_function:test16)
This commit is contained in:
parent
90c2822031
commit
4805f48a4f
|
@ -175,21 +175,28 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
|||
if ( Match( tok, "[=,(] %var1% [,);]", varnames ) )
|
||||
return;
|
||||
|
||||
// Return the memory..
|
||||
if ( Match( tok, "return %var1%", varnames ) )
|
||||
return;
|
||||
|
||||
// Return without deallocating the memory..
|
||||
if ( Alloc != No && alloc_indentlevel >= 0 && Match(tok, "return") )
|
||||
{
|
||||
bool retvar = false;
|
||||
for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next )
|
||||
{
|
||||
if ( Match( tok2, "%var1%", varnames ) )
|
||||
return;
|
||||
if ( tok2->str[0] == ';' )
|
||||
{
|
||||
retvar = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if ( tok2->str[0] == ';' )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! retvar )
|
||||
MemoryLeak( tok, varname );
|
||||
|
||||
if ( indentlevel <= alloc_indentlevel )
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
11
tests.cpp
11
tests.cpp
|
@ -617,6 +617,17 @@ static void memleak_in_function()
|
|||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test15, "" );
|
||||
|
||||
|
||||
const char test16[] = "static char *f()\n"
|
||||
"{\n"
|
||||
" char *s = new char[100];\n"
|
||||
" if ( a == b )\n"
|
||||
" {\n"
|
||||
" return s;\n"
|
||||
" }\n"
|
||||
" return NULL;\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test16, "[test.cpp:8]: Memory leak: s\n" );
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
|
Loading…
Reference in New Issue