diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 7682322ad..40d97c4c2 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -298,7 +298,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) continue; } - if (varname.empty() && Token::Match(tok,"%var% = ")) + if (varname.empty() && Token::Match(tok, "%var% = ")) { varname = tok->str(); allocType = GetAllocationType(tok->tokAt(2)); @@ -1025,8 +1025,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) done = false; } + // Reduce "if if" => "if" + else if (Token::Match(tok2, "if if|callfunc")) + { + Token::eraseTokens(tok2, tok2->tokAt(2)); + done = false; + } - if (Token::simpleMatch(tok2->next(), "if")) + else if (Token::simpleMatch(tok2->next(), "if")) { // Delete empty if that is not followed by an else if (Token::Match(tok2->next(), "if ; !!else")) @@ -1075,13 +1081,6 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) done = false; } - // Reduce "if if" => "if" - else if (Token::simpleMatch(tok2, "if if")) - { - Token::eraseTokens(tok2, tok2->tokAt(2)); - done = false; - } - // Reduce "if return ; alloc ;" => "alloc ;" else if (Token::Match(tok2, "[;{}] if return ; alloc ;")) { @@ -1487,7 +1486,7 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const char varname } simplifycode(tok, all); - // tok->printOut("simplifycode result"); + //tok->printOut("simplifycode result"); // If the variable is not allocated at all => no memory leak if (Token::findmatch(tok, "alloc") == 0) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 7ec0ca077..1f1a7c655 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -203,6 +203,7 @@ private: TEST_CASE(func12); TEST_CASE(func13); TEST_CASE(func14); + TEST_CASE(func15); TEST_CASE(allocfunc1); @@ -1426,6 +1427,21 @@ private: ASSERT_EQUALS("", errout.str()); } + void func15() + { + check("static void a()\n" + "{ return true; }\n" + "\n" + "static void b()\n" + "{\n" + " char *p = malloc(100);\n" + " if (a()) return;\n" // <- memory leak + " free(p);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p\n", errout.str()); + } + + void allocfunc1() {