From 85682ed4298352a69d0cd9658111b8c72d4e1b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 18 Aug 2009 20:49:08 +0200 Subject: [PATCH] Fixed #591 (False positive: Deallocating a deallocated pointer) --- src/checkmemoryleak.cpp | 5 ++++- test/testmemleak.cpp | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index f304cabf2..7a42d89af 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -148,7 +148,10 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok if (! tok2) return No; - if (Token::Match(tok2, (std::string("realloc ( ") + varname).c_str())) + if (! Token::Match(tok2, (std::string("%var% ( ") + varname + " [,)]").c_str())) + return No; + + if (tok2->str() == "realloc") return Malloc; // GTK memory reallocation.. diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index cdbefffcc..69cd715fb 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -209,6 +209,7 @@ private: TEST_CASE(func13); TEST_CASE(func14); TEST_CASE(func15); + TEST_CASE(func16); TEST_CASE(allocfunc1); TEST_CASE(allocfunc2); @@ -1519,6 +1520,26 @@ private: ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p\n", errout.str()); } + void func16() + { + check("static void a( bo_t *p_bo)\n" + "{\n" + " p_bo->buffer = realloc( p_bo->buffer, 100 );\n" + "}\n" + "\n" + "static bo_t * b()\n" + "{\n" + " bo_t *box;\n" + " if( ( box = malloc( sizeof( bo_t ) ) ) )\n" + " {\n" + " a(box);\n" + " a(box);\n" + " }\n" + " return box;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void allocfunc1()