From 0b69a132059135d0a837ac03f41b0373659e6aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 1 Jan 2009 10:14:52 +0000 Subject: [PATCH] Memory leak : Fixed false positives for "memory is used after it has been freed" --- checkmemoryleak.cpp | 9 +++++++-- testmemleak.cpp | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) 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() ); + } + };