CheckMemoryLeak: better handling of switch blocks
This commit is contained in:
parent
46be988e9a
commit
6b12d31f5f
|
@ -135,6 +135,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
||||||
int alloc_indentlevel = -1;
|
int alloc_indentlevel = -1;
|
||||||
int dealloc_indentlevel = -1;
|
int dealloc_indentlevel = -1;
|
||||||
std::list<int> loop_indentlevel;
|
std::list<int> loop_indentlevel;
|
||||||
|
std::list<int> switch_indentlevel;
|
||||||
|
|
||||||
bool isif = false;
|
bool isif = false;
|
||||||
|
|
||||||
|
@ -157,6 +158,9 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
||||||
if ( !loop_indentlevel.empty() && indentlevel <= loop_indentlevel.back() )
|
if ( !loop_indentlevel.empty() && indentlevel <= loop_indentlevel.back() )
|
||||||
loop_indentlevel.pop_back();
|
loop_indentlevel.pop_back();
|
||||||
|
|
||||||
|
if ( !switch_indentlevel.empty() && indentlevel <= switch_indentlevel.back() )
|
||||||
|
switch_indentlevel.pop_back();
|
||||||
|
|
||||||
if ( indentlevel < alloc_indentlevel )
|
if ( indentlevel < alloc_indentlevel )
|
||||||
alloc_indentlevel = -1;
|
alloc_indentlevel = -1;
|
||||||
|
|
||||||
|
@ -261,6 +265,10 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
||||||
if ( Match(tok,"while") || Match(tok,"for") )
|
if ( Match(tok,"while") || Match(tok,"for") )
|
||||||
loop_indentlevel.push_back( indentlevel );
|
loop_indentlevel.push_back( indentlevel );
|
||||||
|
|
||||||
|
// switch..
|
||||||
|
if (Match(tok,"switch"))
|
||||||
|
switch_indentlevel.push_back( indentlevel );
|
||||||
|
|
||||||
// Skip stuff like: if (!var) ...
|
// Skip stuff like: if (!var) ...
|
||||||
if ( Match(tok, "if ( ! %var1% )", varnames) ||
|
if ( Match(tok, "if ( ! %var1% )", varnames) ||
|
||||||
Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) ||
|
Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) ||
|
||||||
|
@ -378,6 +386,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
||||||
// continue/break loop..
|
// continue/break loop..
|
||||||
if (Alloc != No &&
|
if (Alloc != No &&
|
||||||
loop_indentlevel.empty() &&
|
loop_indentlevel.empty() &&
|
||||||
|
switch_indentlevel.empty() &&
|
||||||
(Match(tok,"continue") || Match(tok,"break")))
|
(Match(tok,"continue") || Match(tok,"break")))
|
||||||
{
|
{
|
||||||
MemoryLeak( tok, varname );
|
MemoryLeak( tok, varname );
|
||||||
|
|
22
tests.cpp
22
tests.cpp
|
@ -460,7 +460,8 @@ static void memleak_in_function()
|
||||||
// There are 2 sections:
|
// There are 2 sections:
|
||||||
// * Simple testcases
|
// * Simple testcases
|
||||||
// * if else
|
// * if else
|
||||||
// * for/while..
|
// * for/while
|
||||||
|
// * switch
|
||||||
// * mismatching allocation and deallocation
|
// * mismatching allocation and deallocation
|
||||||
// * garbage collection
|
// * garbage collection
|
||||||
// * arrays
|
// * arrays
|
||||||
|
@ -649,6 +650,25 @@ static void memleak_in_function()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// switch
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
code = "void f()\n"
|
||||||
|
"{\n"
|
||||||
|
" char *str = new char[10];\n"
|
||||||
|
" switch (abc)\n"
|
||||||
|
" {\n"
|
||||||
|
" case 1:\n"
|
||||||
|
" break;\n"
|
||||||
|
" };\n"
|
||||||
|
" delete [] str;\n"
|
||||||
|
"}\n";
|
||||||
|
check( CheckMemoryLeak, __LINE__, code, "" );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
|
|
Loading…
Reference in New Issue