Memory leak: Readded some checks that were removed in r481

This commit is contained in:
Daniel Marjamäki 2008-11-19 18:46:28 +00:00
parent 452ada698e
commit 7a048cf35e
2 changed files with 66 additions and 14 deletions

View File

@ -373,9 +373,32 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
{ {
addtoken("if(!var)"); addtoken("if(!var)");
} }
else if ( Match(tok, "if") || else if ( Match(tok, "if") )
Match(tok, "else") || {
Match(tok, "switch") ) // Check if the condition depends on var somehow..
bool dep = false;
int parlevel = 0;
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
{
if ( Match(tok2,"(") )
++parlevel;
if ( Match(tok2,")") )
{
--parlevel;
if ( parlevel <= 0 )
break;
}
if ( !Match(tok2,".") &&
Match(tok2->next, "%var1%", varnames) &&
!Match(tok2->next, "%var1% .", varnames) )
{
dep = true;
break;
}
}
addtoken( (dep ? "ifv" : "if") );
}
else if ( Match(tok, "else") || Match(tok, "switch") )
{ {
addtoken(tok->str); addtoken(tok->str);
} }
@ -774,20 +797,35 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
MemoryLeak(findmatch(tok, "loop alloc ;"), varname); MemoryLeak(findmatch(tok, "loop alloc ;"), varname);
} }
else if ( _settings._showAll && findmatch(tok, "alloc ; if continue ;") ) else if ( findmatch(tok, "alloc ; if continue ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if continue ;"), 3), varname); MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if continue ;"), 3), varname);
} }
else if ( _settings._showAll && findmatch(tok, "alloc ; if break ;") ) else if ( findmatch(tok, "alloc ; if break ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if break ;"), 3), varname); MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if break ;"), 3), varname);
} }
else if ( _settings._showAll && findmatch(tok, "alloc ; if return ;") ) else if ( findmatch(tok, "alloc ; if return ;") )
{ {
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if return ;"), 3), varname); MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; if return ;"), 3), varname);
} }
else if ( _settings._showAll && findmatch(tok, "alloc ; ifv continue ;") )
{
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv continue ;"), 3), varname);
}
else if ( _settings._showAll && findmatch(tok, "alloc ; ifv break ;") )
{
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv break ;"), 3), varname);
}
else if ( _settings._showAll && findmatch(tok, "alloc ; ifv return ;") )
{
MemoryLeak(Tokenizer::gettok(findmatch(tok, "alloc ; ifv return ;"), 3), varname);
}
else if ( findmatch(tok, "alloc ; return ;") ) else if ( findmatch(tok, "alloc ; return ;") )
{ {

View File

@ -74,7 +74,7 @@ private:
TEST_CASE( use2 ); TEST_CASE( use2 );
TEST_CASE( ifelse1 ); TEST_CASE( ifelse1 );
// TODO TEST_CASE( ifelse2 ); TEST_CASE( ifelse2 );
TEST_CASE( ifelse3 ); TEST_CASE( ifelse3 );
TEST_CASE( ifelse4 ); TEST_CASE( ifelse4 );
TEST_CASE( ifelse5 ); TEST_CASE( ifelse5 );
@ -83,18 +83,19 @@ private:
TEST_CASE( ifelse8 ); TEST_CASE( ifelse8 );
TEST_CASE( ifelse9 ); TEST_CASE( ifelse9 );
// TODO TEST_CASE( if1 ); TEST_CASE( if1 );
TEST_CASE( if2 ); TEST_CASE( if2 );
TEST_CASE( if3 ); TEST_CASE( if3 );
TEST_CASE( if4 ); TEST_CASE( if4 );
TEST_CASE( if5 );
TEST_CASE( forwhile1 ); TEST_CASE( forwhile1 );
// TODO TEST_CASE( forwhile2 ); TEST_CASE( forwhile2 );
TEST_CASE( forwhile3 ); TEST_CASE( forwhile3 );
TEST_CASE( forwhile4 ); TEST_CASE( forwhile4 );
TEST_CASE( forwhile5 ); TEST_CASE( forwhile5 );
TEST_CASE( forwhile6 ); TEST_CASE( forwhile6 );
// TODO TEST_CASE( forwhile7 ); TEST_CASE( forwhile7 );
TEST_CASE( dowhile1 ); TEST_CASE( dowhile1 );
@ -102,7 +103,7 @@ private:
TEST_CASE( switch2 ); TEST_CASE( switch2 );
TEST_CASE( ret1 ); TEST_CASE( ret1 );
// TODO TEST_CASE( ret2 ); TEST_CASE( ret2 );
TEST_CASE( mismatch1 ); TEST_CASE( mismatch1 );
@ -115,11 +116,11 @@ private:
TEST_CASE( class1 ); TEST_CASE( class1 );
TEST_CASE( class2 ); TEST_CASE( class2 );
// TODO TEST_CASE( throw1 ); TEST_CASE( throw1 );
TEST_CASE( linux_list_1 ); TEST_CASE( linux_list_1 );
// TODO TEST_CASE( sizeof1 ); TEST_CASE( sizeof1 );
} }
@ -441,6 +442,19 @@ private:
ASSERT_EQUALS( std::string(""), err ); ASSERT_EQUALS( std::string(""), err );
} }
void if5()
{
check( "void f()\n"
"{\n"
" char *p = malloc(256);\n"
" if (somecondition && !p)\n"
" return;\n"
" free(p);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err );
}