diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index deab72978..7cb90c160 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -930,6 +930,13 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok) done = false; } + // Delete first if in .. "if { dealloc|assign|use ; return ; } if return ;" + if ( TOKEN::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; } if return ;") ) + { + erase(tok2, tok2->tokAt(8)); + done = false; + } + // Delete second case in "case ; case ;" while (TOKEN::Match(tok2, "case ; case ;")) { diff --git a/testmemleak.cpp b/testmemleak.cpp index c75314902..a6e2efe09 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -89,6 +89,7 @@ private: TEST_CASE( if5 ); TEST_CASE( if6 ); // Bug 2432631 TEST_CASE( if7 ); // Bug 2401436 + TEST_CASE( if8 ); // Bug 2458532 TEST_CASE( alwaysTrue ); @@ -535,6 +536,25 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } + void if8() + { + check( "static void f(int i)\n" + "{\n" + " char *c = malloc(50);\n" + " if (i == 1)\n" + " {\n" + " free(c);\n" + " return;\n" + " }\n" + " if (i == 2)\n" + " {\n" + " return;\n" + " }\n" + " free(c);\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:11]: Memory leak: c\n"), errout.str() ); + } +