diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index d3cee674e..3c5f0e4be 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -490,6 +490,13 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list 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) diff --git a/testmemleak.cpp b/testmemleak.cpp index 314fed7be..0e3ce947e 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -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() ); + } +