From c2ea705fd7755d71c660ec37abfa7463d50661bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 9 Nov 2008 08:40:57 +0000 Subject: [PATCH] Memory Leak: Stronger checking. Ignore 'if use ;' and 'if dealloc;'. A leak could occur if it's not executed. --- CheckMemoryLeak.cpp | 8 ++++++++ CommonCheck.cpp | 2 +- testmemleak.cpp | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 6b4eca8ca..8386eb93f 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -517,6 +517,14 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] continue; } } + + // Delete "if dealloc ;" and "if use ;" that is not followed by an else.. + if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) && + !Match(Tokenizer::gettok(tok2,4), "else")) + { + erase(tok2->next, Tokenizer::gettok(tok2,3)); + done = false; + } // Delete if block: "alloc; if return use ;" if (Match(tok2,"alloc ; if return use ;") && !Match(Tokenizer::gettok(tok2,6),"else")) diff --git a/CommonCheck.cpp b/CommonCheck.cpp index 667d98924..55239d5e4 100644 --- a/CommonCheck.cpp +++ b/CommonCheck.cpp @@ -50,7 +50,7 @@ public: _FuncName = FuncName; } - const unsigned int file_id() const { return _FileId; } + unsigned int file_id() const { return _FileId; } const std::string &name() const { return _FuncName; } }; diff --git a/testmemleak.cpp b/testmemleak.cpp index 63d93c649..5ab71332e 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -63,7 +63,9 @@ private: TEST_CASE( simple5 ); TEST_CASE( simple6 ); TEST_CASE( simple7 ); - TEST_CASE( simple8 ); + TEST_CASE( simple8 ); + + TEST_CASE( use1 ); TEST_CASE( ifelse1 ); TEST_CASE( ifelse2 ); @@ -191,6 +193,21 @@ private: + + void use1() + { + check( "void foo()\n" + "{\n" + " char *str = strdup(\"abc\");\n" + " if (somecondition)\n" + " DeleteString(str);\n" + "}\n" ); + + ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() ); + } + + +