Memory leak : Added check "Using resource after it has been released"

This commit is contained in:
Daniel Marjamäki 2008-12-31 09:02:45 +00:00
parent 43150ce9fb
commit 085e97fd36
2 changed files with 34 additions and 3 deletions

View File

@ -1088,11 +1088,26 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
AllocType alloctype = No;
AllocType dealloctype = No;
const TOKEN *result;
TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype );
// tok->printOut( "getcode result" );
//tok->printOut( "getcode result" );
// Simplify the code and check if freed memory is used..
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
{
while ( TOKEN::Match(tok2, "[;{}] ;") )
erase(tok2, tok2->tokAt(2));
}
if ( (result = TOKEN::findmatch(tok, "dealloc [;{}] use ;")) != NULL )
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(result->tokAt(2)) << ": Using \"" << varname << "\" after it has been deallocated / released";
_errorLogger->reportErr( errmsg.str() );
}
simplifycode( tok );
// tok->printOut( "simplifycode result" );
//tok->printOut( "simplifycode result" );
// If the variable is not allocated at all => no memory leak
if (TOKEN::findmatch(tok, "alloc") == 0)
@ -1108,7 +1123,6 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
return;
}
const TOKEN *result;
if ( (result = TOKEN::findmatch(tok, "loop alloc ;")) != NULL )
{
MemoryLeak(result, varname, alloctype);

View File

@ -151,6 +151,8 @@ private:
// TODO TEST_CASE( structmember1 );
TEST_CASE( dealloc_use_1 ); // Deallocate and then use memory
}
@ -1361,6 +1363,21 @@ private:
ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: abc.a\n"), errout.str() );
}
void dealloc_use_1()
{
check( "void f()\n"
"{\n"
" char *s = new char[100];\n"
" delete [] s;\n"
" p = s;\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:5]: Using \"s\" after it has been deallocated / released\n"), errout.str() );
}
};
REGISTER_TEST( TestMemleak )