Memory leaks : Handle assignments better - bug 2395524

This commit is contained in:
Daniel Marjamäki 2008-12-07 07:32:02 +00:00
parent f01cb905f7
commit ea57f2b820
2 changed files with 49 additions and 35 deletions

View File

@ -391,6 +391,12 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
if (dealloctype!=No && dealloctype!=alloc)
MismatchError(tok, callstack, varname);
alloctype = alloc;
}
// assignment..
else
{
addtoken( "assign" );
}
}
@ -508,9 +514,10 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
if ( tok->str() == "throw" )
addtoken("throw");
// Assignment..
if ( TOKEN::Match(tok,"[)=] %var1% [;)]", varnames) )
addtoken("use");
// Assignment..
if ( TOKEN::Match(tok,"[)=] %var1% [;)]", varnames) ||
TOKEN::Match(tok, "%var1% +=|-=", varnames) )
addtoken("use");
// Investigate function calls..
if ( TOKEN::Match(tok, "%var% (") )
@ -971,45 +978,20 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
MemoryLeak(TOKEN::findmatch(tok, "loop alloc ;"), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; if continue ;") )
else if ( TOKEN::findmatch(tok, "alloc ; if break|continue|return ;") )
{
// MemoryLeak(Tokenizer::gettok(TOKEN::findmatch(tok, "alloc ; if continue ;"), 3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if continue ;"))->tokAt(3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if break|continue|return ;"))->tokAt(3), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; if break ;") )
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv break|continue|return ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if break ;"))->tokAt(3), varname);
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv break|continue|return ;"))->tokAt(3), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; if return ;") )
else if ( TOKEN::findmatch(tok, "alloc ; alloc|assign|return ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; if return ;"))->tokAt(3), varname);
}
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv continue ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv continue ;"))->tokAt(3), varname);
}
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv break ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv break ;"))->tokAt(3), varname);
}
else if ( _settings._showAll && TOKEN::findmatch(tok, "alloc ; ifv return ;") )
{
MemoryLeak((TOKEN::findmatch(tok, "alloc ; ifv return ;"))->tokAt(3), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; return ;") )
{
MemoryLeak((TOKEN::findmatch(tok,"alloc ; return ;"))->tokAt(2), varname);
}
else if ( TOKEN::findmatch(tok, "alloc ; alloc") )
{
MemoryLeak((TOKEN::findmatch(tok,"alloc ; alloc"))->tokAt(2), varname);
MemoryLeak((TOKEN::findmatch(tok,"alloc ; alloc|assign|return ;"))->tokAt(2), varname);
}
else if ( ! TOKEN::findmatch(tok,"dealloc") &&

View File

@ -125,7 +125,9 @@ private:
TEST_CASE( sizeof1 );
TEST_CASE( realloc1 );
TEST_CASE( realloc2 );
TEST_CASE( realloc2 );
TEST_CASE( assign );
}
@ -992,6 +994,36 @@ private:
ASSERT_EQUALS( std::string(""), errout.str() );
}
void assign()
{
check( "void foo()\n"
"{\n"
" char *a = (char *)malloc(10);\n"
" a = 0;\n"
" free(a);\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:3]: Memory leak: a\n"), errout.str() );
check( "void foo()\n"
"{\n"
" char *a = (char *)malloc(10);\n"
" char *p = a;\n"
" free(p);\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
check( "void foo()\n"
"{\n"
" char *a = (char *)malloc(10);\n"
" a += 10;\n"
" free(a - 10);\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
}
};