Memory leak : Fixed a problem with a fclose inside an if condition

This commit is contained in:
Daniel Marjamäki 2008-12-28 19:57:50 +00:00
parent 56de5ece91
commit 76239fcdf9
2 changed files with 20 additions and 2 deletions

View File

@ -490,6 +490,13 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
if ( parlevel <= 0 )
break;
}
if ( TOKEN::Match(tok2, "fclose ( %var1% )", varnames) )
{
addtoken( "dealloc" );
addtoken( ";" );
dep = true;
break;
}
if ( (tok2->str() != ".") &&
TOKEN::Match(tok2->next(), "%var1%", varnames) &&
!TOKEN::Match(tok2->next(), "%var1% .", varnames) )
@ -1074,10 +1081,10 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
AllocType dealloctype = No;
TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype );
//tok->printOut( "getcode result" );
// tok->printOut( "getcode result" );
simplifycode( tok );
//tok->printOut( "simplifycode result" );
// tok->printOut( "simplifycode result" );
// If the variable is not allocated at all => no memory leak
if (TOKEN::findmatch(tok, "alloc") == 0)

View File

@ -68,6 +68,7 @@ private:
TEST_CASE( simple7 );
TEST_CASE( simple8 );
TEST_CASE( simple9 ); // Bug 2435468 - member function "free"
TEST_CASE( simple10 ); // fclose in a if condition
TEST_CASE( use1 );
TEST_CASE( use2 );
@ -255,6 +256,16 @@ private:
ASSERT_EQUALS( std::string(""), errout.str() );
}
void simple10()
{
check( "void foo()\n"
"{\n"
" FILE * f = fopen(fname, str);\n"
" if ( fclose(f) != NULL );\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
}