Memory leak : Fixed bug described in issue 2432631. False positive on something like this.. "alloc ; if(!var) alloc;"

This commit is contained in:
Daniel Marjamäki 2008-12-16 17:06:59 +00:00
parent e853f28912
commit a8661baac5
2 changed files with 35 additions and 4 deletions

View File

@ -77,6 +77,8 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T
tok2 = tok2 ? tok2->next() : NULL;
}
if ( ! tok2 )
return No;
if ( ! tok2->isName() )
return No;
// Does tok2 point on "malloc", "strdup" or "kmalloc"..
@ -435,7 +437,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
}
}
addtoken( rhs ? "use" : "assign" );
addtoken( (rhs ? "use" : "assign") );
}
}
@ -649,6 +651,19 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
{
erase( tok2, tok2->tokAt(4) );
done = false;
}
// Reduce "; if(!var) alloc ; !!else" => "; dealloc ; alloc ;"
if ( TOKEN::Match(tok2, "; if(!var) alloc ; !!else") )
{
// Remove the "if(!var)"
erase( tok2, tok2->tokAt(2) );
// Insert "dealloc ;" before the "alloc ;"
tok2->insertToken( ";" );
tok2->insertToken( "dealloc" );
done = false;
}
// TODO Make this more generic. Delete "if ; else use ; use"

View File

@ -85,7 +85,8 @@ private:
TEST_CASE( if2 );
TEST_CASE( if3 );
TEST_CASE( if4 );
TEST_CASE( if5 );
TEST_CASE( if5 );
TEST_CASE( if6 ); // Bug 2432631
TEST_CASE( alwaysTrue );
@ -467,9 +468,24 @@ private:
" return;\n"
" free(p);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err );
ASSERT_EQUALS( std::string(""), errout.str() );
}
void if6()
{
check( "void f()\n"
"{\n"
" FILE *a = 0;\n"
" a = fopen(\"test.txt\", \"rw\");\n"
" if( a == 0 )\n"
" {\n"
" a = fopen(\"test.txt\", \"r\");\n"
" }\n"
"\n"
" fclose( a );\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
}