From 2018c25d20b29ddece70175badf5d59b73dfcdec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 17 Nov 2008 17:31:07 +0000 Subject: [PATCH] Memory leak: moved simplifyTokens rule to '--all' --- CheckMemoryLeak.cpp | 11 +++-------- testmemleak.cpp | 30 ++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 4538fce89..59bbdd6ed 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -571,7 +571,9 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) } // Delete "if dealloc ;" and "if use ;" that is not followed by an else.. - if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) && + // This may cause false positives + if (_settings._showAll && + (Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) && !Match(Tokenizer::gettok(tok2,4), "else")) { erase(tok2->next, Tokenizer::gettok(tok2,3)); @@ -612,13 +614,6 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) done = false; } - // Remove "if dealloc ;" if there is no else after it.. - if (Match(tok2,"if dealloc ;") && !Match(Tokenizer::gettok(tok2,3),"else")) - { - erase( tok2, Tokenizer::gettok(tok2, 2) ); - done = false; - } - // Replace "loop ;" with ";" if ( Match(tok2->next, "loop ;") ) { diff --git a/testmemleak.cpp b/testmemleak.cpp index bf53b5af8..ce4355a76 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -84,8 +84,9 @@ private: TEST_CASE( ifelse9 ); TEST_CASE( if1 ); - TEST_CASE( if2 ); - TEST_CASE( if3 ); + TEST_CASE( if2 ); + TEST_CASE( if3 ); + TEST_CASE( if4 ); TEST_CASE( forwhile1 ); TEST_CASE( forwhile2 ); @@ -220,12 +221,14 @@ private: { check( "void foo()\n" "{\n" - " char *str = strdup(\"abc\");\n" + " char *str;\n" + " if (somecondition)\n" + " str = strdup(\"abc\");\n" " if (somecondition)\n" " DeleteString(str);\n" "}\n" ); - ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() ); + ASSERT_EQUALS( std::string(""), errout.str() ); } @@ -234,7 +237,7 @@ private: check( "void foo()\n" "{\n" " char *str = strdup(\"abc\");\n" - " if ( abc ) { memset(str, 0, 3); }\n" + " if ( abc ) { memset(str, 0, 3); }\n" " *somestr = str;\n" "}\n" ); @@ -380,7 +383,7 @@ private: " delete [] s;\n" " }\n" "}\n" ); - ASSERT_EQUALS( std::string("[test.cpp:8]: Memory leak: s\n"), errout.str() ); + ASSERT_EQUALS( std::string(""), errout.str() ); } @@ -423,6 +426,21 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } + void if4() + { + check( "void f()\n" + "{\n" + " char *s;\n" + " bool b = true;\n" + " if (b && (s = malloc(256)))\n" + " ;\n" + " if (b)\n" + " free(s);\n" + "}\n" ); + std::string err( errout.str() ); + ASSERT_EQUALS( std::string(""), err ); + } +