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:
parent
cc569d164d
commit
8ff5124233
|
@ -576,7 +576,13 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
|
||||||
if ( TOKEN::Match(tok,"[)=] %var1% [+;)]", varnames) ||
|
if ( TOKEN::Match(tok,"[)=] %var1% [+;)]", varnames) ||
|
||||||
TOKEN::Match(tok, "%var1% +=|-=", varnames) ||
|
TOKEN::Match(tok, "%var1% +=|-=", varnames) ||
|
||||||
TOKEN::Match(tok, "+=|<< %var1% ;", varnames) )
|
TOKEN::Match(tok, "+=|<< %var1% ;", varnames) )
|
||||||
|
{
|
||||||
addtoken("use");
|
addtoken("use");
|
||||||
|
}
|
||||||
|
else if ( TOKEN::Match(tok, "[;{}=(,+-*/] %var1% [", varnames) )
|
||||||
|
{
|
||||||
|
addtoken("use_");
|
||||||
|
}
|
||||||
|
|
||||||
// Investigate function calls..
|
// Investigate function calls..
|
||||||
if ( TOKEN::Match(tok, "%var% (") )
|
if ( TOKEN::Match(tok, "%var% (") )
|
||||||
|
@ -1101,7 +1107,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
|
||||||
while ( TOKEN::Match(tok2, "[;{}] ;") )
|
while ( TOKEN::Match(tok2, "[;{}] ;") )
|
||||||
erase(tok2, tok2->tokAt(2));
|
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;
|
std::ostringstream errmsg;
|
||||||
errmsg << _tokenizer->fileLine(result->tokAt(2)) << ": Using \"" << varname << "\" after it has been deallocated / released";
|
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")
|
if (tok2->str() == "&use")
|
||||||
tok2->str("use");
|
tok2->str("use");
|
||||||
else if (tok2->str() == "&use2")
|
else if (tok2->str() == "&use2" || tok2->str() == "use_")
|
||||||
tok2->str(";");
|
tok2->str(";");
|
||||||
else if (tok2->str() == "recursive" || tok2->str() == "dealloc_")
|
else if (tok2->str() == "recursive" || tok2->str() == "dealloc_")
|
||||||
tok2->str("dealloc");
|
tok2->str("dealloc");
|
||||||
|
|
|
@ -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_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_3 ); // Deallocate and then use memory. No error
|
||||||
TEST_CASE( dealloc_use_4 );
|
TEST_CASE( dealloc_use_4 );
|
||||||
|
TEST_CASE( dealloc_use_5 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1422,6 +1423,17 @@ private:
|
||||||
ASSERT_EQUALS( std::string(""), errout.str() );
|
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 )
|
REGISTER_TEST( TestMemleak )
|
||||||
|
|
Loading…
Reference in New Issue