From dc2a0a6468ce083a89e84375360b4462dbf367fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 6 Jul 2010 21:36:50 +0200 Subject: [PATCH] Fixed #1789 (false positive: memory leak (reallocation in subfunction through parameter)) --- lib/checkmemoryleak.cpp | 36 +++++++++++++++++++++--------------- test/testmemleak.cpp | 2 +- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index ff758186a..75392c8d7 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -526,27 +526,33 @@ const char *CheckMemoryLeak::functionArgAlloc(const Token *tok, unsigned int tar break; --indentlevel; } - if (Token::Match(tok, "free ( * %varid% )", varid)) + else if (tok->varId() == varid) { - realloc = 1; - allocType = No; - } - if (Token::Match(tok, "* %varid% =", varid)) - { - allocType = getAllocationType(tok->tokAt(3), varid); - if (allocType == No) + if (Token::Match(tok->tokAt(-3), "free ( * %varid% )", varid)) { - allocType = getReallocationType(tok->tokAt(3), varid); + realloc = 1; + allocType = No; } - if (allocType != No) + else if (Token::Match(tok->previous(), "* %varid% =", varid)) { - if (realloc) - return "realloc"; - return "alloc"; + allocType = getAllocationType(tok->tokAt(2), varid); + if (allocType == No) + { + allocType = getReallocationType(tok->tokAt(2), varid); + } + if (allocType != No) + { + if (realloc) + return "realloc"; + return "alloc"; + } + } + else + { + // unhandled variable usage: bailout + return ""; } } - if (tok->str() == "return") - return ""; } return ""; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 656205760..4c603f887 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1820,7 +1820,7 @@ private: " foo(&tmp);\n" " free(tmp);\n" "}\n"); - TODO_ASSERT_EQUALS(std::string(""), errout.str()); + ASSERT_EQUALS(std::string(""), errout.str()); }