Memory leak : Fixed false positives for "memory is used after it has been freed"

This commit is contained in:
Daniel Marjamäki 2009-01-01 10:14:52 +00:00
parent 784fc0d33e
commit 0b69a13205
2 changed files with 20 additions and 2 deletions

View File

@ -602,9 +602,12 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
// Linux lists.. // Linux lists..
if ( TOKEN::Match( tok, "[=(,] & %var1% [.[]", varnames ) ) if ( TOKEN::Match( tok, "[=(,] & %var1% [.[]", varnames ) )
{ {
// todo: better checking
addtoken("&use"); addtoken("&use");
} }
else if ( TOKEN::Match( tok, "[=(,] & %var1% [,)]", varnames ) )
{
addtoken("&use2");
}
} }
return rethead; return rethead;
@ -1092,11 +1095,13 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
_errorLogger->reportErr( errmsg.str() ); _errorLogger->reportErr( errmsg.str() );
} }
// Replace "&use" with "use" // Replace "&use" with "use". Replace "&use2" with ";"
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
{ {
if (tok2->str() == "&use") if (tok2->str() == "&use")
tok2->str("use"); tok2->str("use");
else if (tok2->str() == "&use2")
tok2->str(";");
} }
simplifycode( tok ); simplifycode( tok );

View File

@ -154,6 +154,7 @@ private:
TEST_CASE( dealloc_use_1 ); // Deallocate and then use memory TEST_CASE( dealloc_use_1 ); // Deallocate and then use memory
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
} }
@ -1389,6 +1390,18 @@ private:
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void dealloc_use_3()
{
check( "void foo()\n"
"{\n"
" char *str = 0;\n"
" free(str);\n"
" f1(&str);\n"
" f2(str);\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
}
}; };