Memory leak: Handle conditions that are always true / false

This commit is contained in:
Daniel Marjamäki 2008-11-22 11:30:50 +00:00
parent 2db69e6072
commit e33985dbf0
2 changed files with 35 additions and 3 deletions

View File

@ -382,6 +382,14 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
{
addtoken("if(!var)");
}
else if ( Tokenizer::Match(tok, "if (") && _tokenizer->alwaysTrue(tok->next) )
{
addtoken("if(true)");
}
else if ( Tokenizer::Match(tok, "if (") && _tokenizer->alwaysTrue(tok->next) )
{
addtoken("if(false)");
}
else if ( Tokenizer::Match(tok, "if") )
{
// Check if the condition depends on var somehow..
@ -599,6 +607,8 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
if ( Tokenizer::Match(tok2,"[;{}] if ;") ||
Tokenizer::Match(tok2,"[;{}] if(var) ;") ||
Tokenizer::Match(tok2,"[;{}] if(!var) ;") ||
Tokenizer::Match(tok2,"[;{}] if(true) ;") ||
Tokenizer::Match(tok2,"[;{}] if(false) ;") ||
Tokenizer::Match(tok2,"[;{}] ifv ;") )
{
if ( ! Tokenizer::Match(Tokenizer::gettok(tok2,3), "else") )

View File

@ -86,6 +86,8 @@ private:
TEST_CASE( if4 );
TEST_CASE( if5 );
TEST_CASE( alwaysTrue );
TEST_CASE( forwhile1 );
TEST_CASE( forwhile2 );
TEST_CASE( forwhile3 );
@ -457,6 +459,26 @@ private:
void alwaysTrue()
{
check( "void f()\n"
"{\n"
" char *p = 0;\n"
" for (;;)\n"
" {\n"
" p = malloc(256);\n"
" if (1)\n"
" break;\n"
" }\n"
" free(p);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err );
}
void forwhile1()
{