From a8661baac509e8b60c23da289898db0b93bf5a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 16 Dec 2008 17:06:59 +0000 Subject: [PATCH] Memory leak : Fixed bug described in issue 2432631. False positive on something like this.. "alloc ; if(!var) alloc;" --- checkmemoryleak.cpp | 17 ++++++++++++++++- testmemleak.cpp | 22 +++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index f92bb4071..491ad2d6f 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -77,6 +77,8 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T tok2 = tok2 ? tok2->next() : NULL; } if ( ! tok2 ) + return No; + if ( ! tok2->isName() ) return No; // Does tok2 point on "malloc", "strdup" or "kmalloc".. @@ -435,7 +437,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list } } - addtoken( rhs ? "use" : "assign" ); + addtoken( (rhs ? "use" : "assign") ); } } @@ -649,6 +651,19 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) { erase( tok2, tok2->tokAt(4) ); done = false; + } + + // Reduce "; if(!var) alloc ; !!else" => "; dealloc ; alloc ;" + if ( TOKEN::Match(tok2, "; if(!var) alloc ; !!else") ) + { + // Remove the "if(!var)" + erase( tok2, tok2->tokAt(2) ); + + // Insert "dealloc ;" before the "alloc ;" + tok2->insertToken( ";" ); + tok2->insertToken( "dealloc" ); + + done = false; } // TODO Make this more generic. Delete "if ; else use ; use" diff --git a/testmemleak.cpp b/testmemleak.cpp index 540f89baf..97e2d2059 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -85,7 +85,8 @@ private: TEST_CASE( if2 ); TEST_CASE( if3 ); TEST_CASE( if4 ); - TEST_CASE( if5 ); + TEST_CASE( if5 ); + TEST_CASE( if6 ); // Bug 2432631 TEST_CASE( alwaysTrue ); @@ -467,9 +468,24 @@ private: " return;\n" " free(p);\n" "}\n" ); - std::string err( errout.str() ); - ASSERT_EQUALS( std::string(""), err ); + ASSERT_EQUALS( std::string(""), errout.str() ); } + + void if6() + { + check( "void f()\n" + "{\n" + " FILE *a = 0;\n" + " a = fopen(\"test.txt\", \"rw\");\n" + " if( a == 0 )\n" + " {\n" + " a = fopen(\"test.txt\", \"r\");\n" + " }\n" + "\n" + " fclose( a );\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + }