diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index ad3355ef6..7b1354cae 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -602,9 +602,12 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list // Linux lists.. if ( TOKEN::Match( tok, "[=(,] & %var1% [.[]", varnames ) ) { - // todo: better checking addtoken("&use"); } + else if ( TOKEN::Match( tok, "[=(,] & %var1% [,)]", varnames ) ) + { + addtoken("&use2"); + } } return rethead; @@ -1092,11 +1095,13 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const _errorLogger->reportErr( errmsg.str() ); } - // Replace "&use" with "use" + // Replace "&use" with "use". Replace "&use2" with ";" for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) { if (tok2->str() == "&use") tok2->str("use"); + else if (tok2->str() == "&use2") + tok2->str(";"); } simplifycode( tok ); diff --git a/testmemleak.cpp b/testmemleak.cpp index f9de11f80..425004e70 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -154,6 +154,7 @@ private: TEST_CASE( dealloc_use_1 ); // Deallocate and then use memory TEST_CASE( dealloc_use_2 ); // Deallocate and then use memory. No error if "use" is &var + TEST_CASE( dealloc_use_3 ); // Deallocate and then use memory. No error } @@ -1389,6 +1390,18 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } + void dealloc_use_3() + { + check( "void foo()\n" + "{\n" + " char *str = 0;\n" + " free(str);\n" + " f1(&str);\n" + " f2(str);\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + };