Memory leak: Improved the checking of usage after free (if str is freed then "char c = str[0];" is illegal)

This commit is contained in:
Daniel Marjamäki 2009-01-02 08:00:12 +00:00
parent cc569d164d
commit 8ff5124233
2 changed files with 20 additions and 2 deletions

View File

@ -576,7 +576,13 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
if ( TOKEN::Match(tok,"[)=] %var1% [+;)]", varnames) ||
TOKEN::Match(tok, "%var1% +=|-=", varnames) ||
TOKEN::Match(tok, "+=|<< %var1% ;", varnames) )
{
addtoken("use");
}
else if ( TOKEN::Match(tok, "[;{}=(,+-*/] %var1% [", varnames) )
{
addtoken("use_");
}
// Investigate function calls..
if ( TOKEN::Match(tok, "%var% (") )
@ -1101,7 +1107,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
while ( TOKEN::Match(tok2, "[;{}] ;") )
erase(tok2, tok2->tokAt(2));
}
if ( (result = TOKEN::findmatch(tok, "dealloc [;{}] use ;")) != NULL )
if ( (result = TOKEN::findmatch(tok, "dealloc [;{}] use|use_ ;")) != NULL )
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(result->tokAt(2)) << ": Using \"" << varname << "\" after it has been deallocated / released";
@ -1113,7 +1119,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
{
if (tok2->str() == "&use")
tok2->str("use");
else if (tok2->str() == "&use2")
else if (tok2->str() == "&use2" || tok2->str() == "use_")
tok2->str(";");
else if (tok2->str() == "recursive" || tok2->str() == "dealloc_")
tok2->str("dealloc");

View File

@ -156,6 +156,7 @@ private:
TEST_CASE( dealloc_use_2 ); // Deallocate and then use memory. No error if "use" is &var
TEST_CASE( dealloc_use_3 ); // Deallocate and then use memory. No error
TEST_CASE( dealloc_use_4 );
TEST_CASE( dealloc_use_5 );
}
@ -1422,6 +1423,17 @@ private:
ASSERT_EQUALS( std::string(""), errout.str() );
}
void dealloc_use_5()
{
check( "void foo()\n"
"{\n"
" char *str = 0;\n"
" free(str);\n"
" char c = str[10];\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:5]: Using \"str\" after it has been deallocated / released\n"), errout.str() );
}
};
REGISTER_TEST( TestMemleak )