From a46de47158a51d8bf635019251759e5fe5fb8a25 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Fri, 1 May 2009 14:39:57 +0300 Subject: [PATCH] Fix ticket #196 (False positive: Resource leak) http://apps.sourceforge.net/trac/cppcheck/ticket/196 --- src/checkmemoryleak.cpp | 23 +++++++++++++++++------ test/testmemleak.cpp | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 4ddc4b45d..01c931f50 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -331,10 +331,7 @@ bool CheckMemoryLeakClass::notvar(const Token *tok, const char *varnames[], bool const std::string end(endpar ? " &&|)" : " [;)&|]"); return bool(Token::Match(tok, ("! " + varname + end).c_str()) || - Token::Match(tok, ("! ( " + varname + " )" + end).c_str()) || - Token::Match(tok, ("0 == " + varname + end).c_str()) || - Token::Match(tok, (varname + " == 0" + end).c_str()) || - Token::Match(tok, ("( " + varname + " ) == 0" + end).c_str())); + Token::Match(tok, ("! ( " + varname + " )" + end).c_str())); } Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype, bool classmember, bool &all, unsigned int sz) @@ -557,7 +554,21 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list break; } } - addtoken((dep ? "ifv" : "if")); + + if (Token::simpleMatch(tok, std::string("if ( ! " + varnameStr + " &&").c_str())) + { + addtoken("if(!var)"); + } + else if (tok->next() && + tok->next()->link() && + Token::simpleMatch(tok->next()->link()->previous()->previous()->previous(), std::string("&& ! " + varnameStr).c_str())) + { + addtoken("if(!var)"); + } + else + { + addtoken((dep ? "ifv" : "if")); + } } } @@ -1255,7 +1266,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope(const Token *Tok1, const c } 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 dd3f335c9..837d41b08 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2108,7 +2108,7 @@ private: "\n" " delete [] a;\n" "}\n", true); - TODO_ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("", errout.str()); } };