From 0361c9d33869b49419a1780a2b9440687ececdfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 8 Feb 2009 11:59:04 +0000 Subject: [PATCH] Memory leak: Handling function that can't be traced into --- src/checkmemoryleak.cpp | 10 ++++++---- test/testmemleak.cpp | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 6aa4f0ce9..8f5fb6595 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -196,7 +196,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType(const const char * CheckMemoryLeakClass::call_func(const Token *tok, std::list callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype, bool &all, unsigned int sz) { // Keywords that are not function calls.. - if (Token::Match(tok, "if|for|while")) + if (Token::Match(tok, "if|for|while|return|switch")) return 0; // String functions that are not allocating nor deallocating memory.. @@ -221,7 +221,7 @@ const char * CheckMemoryLeakClass::call_func(const Token *tok, std::list 2) return "dealloc_"; - const char *funcname = tok->aaaa(); + const std::string funcname(tok->str()); for (std::list::const_iterator it = callstack.begin(); it != callstack.end(); ++it) { if ((*it)->str() == funcname) @@ -250,7 +250,9 @@ const char * CheckMemoryLeakClass::call_func(const Token *tok, std::listGetFunctionTokenByName(funcname); + const Token *ftok = _tokenizer->GetFunctionTokenByName(funcname.c_str()); const char *parname = Tokenizer::getParameterName(ftok, par); if (! parname) return "recursive"; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 45535597e..b88ef01ab 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -188,7 +188,8 @@ private: TEST_CASE(malloc_constant_1); // Check that the malloc constant matches the type // Calls to unknown functions.. they may throw exception, quit the program, etc - // TODO TEST_CASE(unknownFunction1); + TEST_CASE(unknownFunction1); + // TODO TEST_CASE(unknownFunction2); } @@ -1828,20 +1829,31 @@ private: void unknownFunction1() { - const char code[] = "void foo()\n" - "{\n" - " int *p = new int[100];\n" - " if (abc)\n" - " {\n" - " delete [] p;\n" - " ThrowException();\n" - " }\n" - " delete [] p;\n" - "}\n"; - check(code, false); + check("void foo()\n" + "{\n" + " int *p = new int[100];\n" + " if (abc)\n" + " {\n" + " delete [] p;\n" + " ThrowException();\n" + " }\n" + " delete [] p;\n" + "}\n"); ASSERT_EQUALS("", errout.str()); - check(code, true); - ASSERT_EQUALS("[test.cpp:9]: (all) Deallocating a deallocated pointer: p\n", errout.str()); + } + + void unknownFunction2() + { + check("void foo()\n" + "{\n" + " int *p = new int[100];\n" + " if (abc)\n" + " {\n" + " delete [] p;\n" + " ThrowException();\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:9] (error) Memory leak: p\n", errout.str()); } };