From 80be31de13bd5281ae967af5164cc28f2ad222ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 8 Sep 2010 20:03:22 +0200 Subject: [PATCH] Fixed #2037 (memleak not detected in exit path when variable used) --- lib/checkmemoryleak.cpp | 27 +++++++++++++++------------ test/testmemleak.cpp | 8 +++++--- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 67a17fb9d..0accf25c0 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1368,21 +1368,24 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listtokAt(2); tok2; tok2 = tok2->next()) + if (!test_white_list(tok->str())) { - if (tok2->str() == "(") - ++innerParlevel; - else if (tok2->str() == ")") + int innerParlevel = 1; + for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { - --innerParlevel; - if (innerParlevel <= 0) + if (tok2->str() == "(") + ++innerParlevel; + else if (tok2->str() == ")") + { + --innerParlevel; + if (innerParlevel <= 0) + break; + } + if (tok2->varId() == varid) + { + addtoken(&rettail, tok, "::use"); break; - } - if (tok2->varId() == varid) - { - addtoken(&rettail, tok, "::use"); - break; + } } } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index e04e527f0..deab61f41 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -410,7 +410,7 @@ private: - std::string getcode(const char code[], const char varname[]) const + std::string getcode(const char code[], const char varname[], bool classfunc=false) const { // Tokenize.. Tokenizer tokenizer; @@ -427,7 +427,7 @@ private: callstack.push_back(0); CheckMemoryLeak::AllocType allocType, deallocType; allocType = deallocType = CheckMemoryLeak::No; - Token *tokens = checkMemoryLeak.getcode(tokenizer.tokens(), callstack, varId, allocType, deallocType, false, 1); + Token *tokens = checkMemoryLeak.getcode(tokenizer.tokens(), callstack, varId, allocType, deallocType, classfunc, 1); // stringify.. std::ostringstream ret; @@ -442,7 +442,6 @@ private: - void testgetcode() { // alloc; @@ -562,6 +561,9 @@ private: // fcloseall.. ASSERT_EQUALS(";;alloc;;", getcode("char *s; s = malloc(10); fcloseall();", "s")); ASSERT_EQUALS(";;alloc;dealloc;", getcode("FILE *f; f = fopen(a,b); fcloseall();", "f")); + + // call memcpy in class function.. + ASSERT_EQUALS(";;alloc;;", getcode("char *s; s = new char[10]; memcpy(s,a);", "s", true)); }