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

@ -392,6 +392,12 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
MismatchError(tok, callstack, varname);
alloctype = alloc;
}
// assignment..
else
{
addtoken( "assign" );
}
}
AllocType dealloc = GetDeallocationType(tok, varnames);
@ -509,7 +515,8 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
addtoken("throw");
// Assignment..
if ( TOKEN::Match(tok,"[)=] %var1% [;)]", varnames) )
if ( TOKEN::Match(tok,"[)=] %var1% [;)]", varnames) ||
TOKEN::Match(tok, "%var1% +=|-=", varnames) )
addtoken("use");
// Investigate function calls..
@ -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

@ -126,6 +126,8 @@ private:
TEST_CASE( realloc1 );
TEST_CASE( realloc2 );
TEST_CASE( assign );
}
@ -993,6 +995,36 @@ private:
}
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() );
}
};
REGISTER_TEST( TestMemleak )