From 492082f4f26c5126f3d268236c3fdf6e1a358610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 25 Dec 2008 08:27:07 +0000 Subject: [PATCH] Memory leaks : Fixed false positive with return (Bug 2458436) --- checkmemoryleak.cpp | 14 ++++++++++++++ testmemleak.cpp | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/checkmemoryleak.cpp b/checkmemoryleak.cpp index 059a381f0..8f93caa9a 100644 --- a/checkmemoryleak.cpp +++ b/checkmemoryleak.cpp @@ -549,6 +549,20 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list if ( TOKEN::Match(tok, "return %var1%", varnames) || TOKEN::Match(tok, "return & %var1%", varnames) ) addtoken("use"); + if (TOKEN::simpleMatch(tok->next(), "(")) + { + for (const TOKEN *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next() ) + { + if ( tok2->str() == "(" || tok2->str() == ")" ) + break; + + if ( tok2->str() == varname ) + { + addtoken("use"); + break; + } + } + } } // throw.. diff --git a/testmemleak.cpp b/testmemleak.cpp index 842c945a2..816daf7ec 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -111,6 +111,7 @@ private: TEST_CASE( ret2 ); TEST_CASE( ret3 ); TEST_CASE( ret4 ); + TEST_CASE( ret5 ); // Bug 2458436 - return use TEST_CASE( mismatch1 ); @@ -818,6 +819,19 @@ private: ASSERT_EQUALS( std::string("[test.cpp:4]: Resource leak: p\n"), errout.str() ); } + void ret5() + { + check( "static char * f()\n" + "{\n" + " char *c = new char[50];\n" + " return (c ? c : NULL);\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + + + + void mismatch1() { check( "void f()\n"