From 2c74d1c0de925084a1501381c27b69fe3f823cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 9 Nov 2008 17:27:23 +0000 Subject: [PATCH] Memory leak: Ensure that simple memory leak is detected --- CheckMemoryLeak.cpp | 3 +++ testmemleak.cpp | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 16d32e01e..0b75a7525 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -181,6 +181,9 @@ static std::list callstack; static const char * call_func( const TOKEN *tok, const char *varnames[] ) { + if (Match(tok,"if") || Match(tok,"for") || Match(tok,"while")) + return 0; + if (GetAllocationType(tok)!=No || GetDeallocationType(tok,varnames)!=No) return 0; diff --git a/testmemleak.cpp b/testmemleak.cpp index daf89017e..d2f78bbe9 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -80,6 +80,8 @@ private: TEST_CASE( ifelse8 ); TEST_CASE( ifelse9 ); + TEST_CASE( if1 ); + TEST_CASE( forwhile1 ); TEST_CASE( forwhile2 ); TEST_CASE( forwhile3 ); @@ -368,6 +370,23 @@ private: + void if1() + { + check( "void f()\n" + "{\n" + " struct abc *p = new abc;\n" + " p->a = new char[100];\n" + " if ( ! p->a )\n" + " return;\n" + " foo(p);\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: p\n"), errout.str() ); + } + + + + + void forwhile1() {