diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index d110c41eb..fb8f64cd6 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -602,7 +602,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list if ( TOKEN::Match( tok, "[=(,] & %var1% [.[]", varnames ) ) { // todo: better checking - addtoken("use"); + addtoken("&use"); } } @@ -1091,6 +1091,13 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const _errorLogger->reportErr( errmsg.str() ); } + // Replace "&use" with "use" + for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + { + if (tok2->str() == "&use") + tok2->setstr("use"); + } + simplifycode( tok ); //tok->printOut( "simplifycode result" ); diff --git a/testmemleak.cpp b/testmemleak.cpp index 3370b5ea0..f9de11f80 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -153,6 +153,7 @@ private: // TODO TEST_CASE( structmember1 ); 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 } @@ -1377,6 +1378,17 @@ private: ASSERT_EQUALS( std::string("[test.cpp:5]: Using \"s\" after it has been deallocated / released\n"), errout.str() ); } + void dealloc_use_2() + { + check( "void f()\n" + "{\n" + " char *str;\n" + " free(str);\n" + " foo(&str);\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + };