Memory leak: Better handling when calling functions

This commit is contained in:
Daniel Marjamäki 2008-11-21 18:02:20 +00:00
parent 7f4cc495df
commit a82b46dad6
2 changed files with 22 additions and 4 deletions

View File

@ -180,13 +180,13 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
return 0; return 0;
if ( callstack.size() > 2 ) if ( callstack.size() > 2 )
return 0; return "dealloc";
const char *funcname = tok->str; const char *funcname = tok->str;
for ( std::list<const TOKEN *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it ) for ( std::list<const TOKEN *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it )
{ {
if ( std::string(funcname) == (*it)->str ) if ( std::string(funcname) == (*it)->str )
return 0; return "dealloc";
} }
callstack.push_back(tok); callstack.push_back(tok);
@ -220,7 +220,7 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
simplifycode( func ); simplifycode( func );
const char *ret = 0; const char *ret = 0;
if (Tokenizer::findmatch(func, "goto")) if (Tokenizer::findmatch(func, "goto"))
ret = 0; // TODO : "goto" isn't handled well ret = "dealloc"; // TODO : "goto" isn't handled well
else if (Tokenizer::findmatch(func, "use")) else if (Tokenizer::findmatch(func, "use"))
ret = "use"; ret = "use";
else if (Tokenizer::findmatch(func, "dealloc")) else if (Tokenizer::findmatch(func, "dealloc"))

View File

@ -111,7 +111,8 @@ private:
TEST_CASE( func2 ); TEST_CASE( func2 );
TEST_CASE( func3 ); TEST_CASE( func3 );
TEST_CASE( func4 ); TEST_CASE( func4 );
TEST_CASE( func5 ); TEST_CASE( func5 );
TEST_CASE( func6 );
TEST_CASE( class1 ); TEST_CASE( class1 );
TEST_CASE( class2 ); TEST_CASE( class2 );
@ -745,6 +746,23 @@ private:
ASSERT_EQUALS( std::string("[test.cpp:9] -> [test.cpp:3]: Mismatching allocation and deallocation: str\n"), err ); ASSERT_EQUALS( std::string("[test.cpp:9] -> [test.cpp:3]: Mismatching allocation and deallocation: str\n"), err );
} }
void func6()
{
check( "static void foo(char *str)\n"
"{\n"
" goto abc;\n"
"}\n"
"\n"
"static void f()\n"
"{\n"
" char *p = new char[100];\n"
" foo(p);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err );
}
/* /*