From d0f3dccc6d05da857055709200735f26d0605293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 5 Jun 2009 08:56:46 +0200 Subject: [PATCH] Fixed #354 (false positive: memory leak) --- src/checkmemoryleak.cpp | 11 +++++++++-- test/testmemleak.cpp | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 95a3e447c..ce62c8c59 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -657,10 +657,17 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list else if (Token::simpleMatch(tok->next(), "(")) { + int parlevel = 1; for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { - if (tok2->str() == "(" || tok2->str() == ")") - break; + if (tok2->str() == "(") + ++parlevel; + else if (tok2->str() == ")") + { + if (parlevel <= 1) + break; + --parlevel; + } if (tok2->str() == varname) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 82f3448d0..1f6f71fc5 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -123,6 +123,7 @@ private: TEST_CASE(ret5); // Bug 2458436 - return use TEST_CASE(ret6); TEST_CASE(ret7); + TEST_CASE(ret8); TEST_CASE(mismatch1); TEST_CASE(mismatch2); @@ -1094,6 +1095,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void ret8() + { + check("char *foo()\n" + "{\n" + " char *c = new char[50];\n" + " return ((char *)(c+1));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void mismatch1() {