Memory leak: moved simplifyTokens rule to '--all'

This commit is contained in:
Daniel Marjamäki 2008-11-17 17:31:07 +00:00
parent 7e8b3c86f6
commit 2018c25d20
2 changed files with 27 additions and 14 deletions

View File

@ -571,7 +571,9 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
}
// Delete "if dealloc ;" and "if use ;" that is not followed by an else..
if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) &&
// This may cause false positives
if (_settings._showAll &&
(Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) &&
!Match(Tokenizer::gettok(tok2,4), "else"))
{
erase(tok2->next, Tokenizer::gettok(tok2,3));
@ -612,13 +614,6 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false;
}
// Remove "if dealloc ;" if there is no else after it..
if (Match(tok2,"if dealloc ;") && !Match(Tokenizer::gettok(tok2,3),"else"))
{
erase( tok2, Tokenizer::gettok(tok2, 2) );
done = false;
}
// Replace "loop ;" with ";"
if ( Match(tok2->next, "loop ;") )
{

View File

@ -84,8 +84,9 @@ private:
TEST_CASE( ifelse9 );
TEST_CASE( if1 );
TEST_CASE( if2 );
TEST_CASE( if3 );
TEST_CASE( if2 );
TEST_CASE( if3 );
TEST_CASE( if4 );
TEST_CASE( forwhile1 );
TEST_CASE( forwhile2 );
@ -220,12 +221,14 @@ private:
{
check( "void foo()\n"
"{\n"
" char *str = strdup(\"abc\");\n"
" char *str;\n"
" if (somecondition)\n"
" str = strdup(\"abc\");\n"
" if (somecondition)\n"
" DeleteString(str);\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() );
ASSERT_EQUALS( std::string(""), errout.str() );
}
@ -234,7 +237,7 @@ private:
check( "void foo()\n"
"{\n"
" char *str = strdup(\"abc\");\n"
" if ( abc ) { memset(str, 0, 3); }\n"
" if ( abc ) { memset(str, 0, 3); }\n"
" *somestr = str;\n"
"}\n" );
@ -380,7 +383,7 @@ private:
" delete [] s;\n"
" }\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:8]: Memory leak: s\n"), errout.str() );
ASSERT_EQUALS( std::string(""), errout.str() );
}
@ -423,6 +426,21 @@ private:
ASSERT_EQUALS( std::string(""), errout.str() );
}
void if4()
{
check( "void f()\n"
"{\n"
" char *s;\n"
" bool b = true;\n"
" if (b && (s = malloc(256)))\n"
" ;\n"
" if (b)\n"
" free(s);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err );
}