Memory leak: Removed false positives. 'use ; use ;' is not always the same as 'use ;'

This commit is contained in:
Daniel Marjamäki 2008-11-09 10:09:42 +00:00
parent c2ea705fd7
commit 26bfab1c7e
2 changed files with 41 additions and 27 deletions

View File

@ -517,14 +517,14 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
continue;
}
}
// Delete "if dealloc ;" and "if use ;" that is not followed by an else..
if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) &&
!Match(Tokenizer::gettok(tok2,4), "else"))
{
erase(tok2->next, Tokenizer::gettok(tok2,3));
done = false;
}
// Delete "if dealloc ;" and "if use ;" that is not followed by an else..
if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) &&
!Match(Tokenizer::gettok(tok2,4), "else"))
{
erase(tok2->next, Tokenizer::gettok(tok2,3));
done = false;
}
// Delete if block: "alloc; if return use ;"
if (Match(tok2,"alloc ; if return use ;") && !Match(Tokenizer::gettok(tok2,6),"else"))
@ -596,7 +596,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
}
// Delete second use in "use ; use ;"
while (Match(tok2, "use ; use ;"))
while (Match(tok2, "[;{}] use ; use ;"))
{
erase(tok2, Tokenizer::gettok(tok2,3));
done = false;

View File

@ -39,7 +39,7 @@ private:
{
// Tokenize..
tokens = tokens_back = NULL;
std::istringstream istr(code);
std::istringstream istr(code);
Tokenizer tokenizer;
tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList();
@ -63,9 +63,10 @@ private:
TEST_CASE( simple5 );
TEST_CASE( simple6 );
TEST_CASE( simple7 );
TEST_CASE( simple8 );
TEST_CASE( simple8 );
TEST_CASE( use1 );
TEST_CASE( use2 );
TEST_CASE( ifelse1 );
TEST_CASE( ifelse2 );
@ -193,21 +194,34 @@ private:
void use1()
{
check( "void foo()\n"
"{\n"
" char *str = strdup(\"abc\");\n"
" if (somecondition)\n"
" DeleteString(str);\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() );
}
void use1()
{
check( "void foo()\n"
"{\n"
" char *str = strdup(\"abc\");\n"
" if (somecondition)\n"
" DeleteString(str);\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() );
}
void use2()
{
check( "void foo()\n"
"{\n"
" char *str = strdup(\"abc\");\n"
" if ( abc ) { memset(str, 0, 3); }\n"
" *somestr = str;\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
}