From eedb5e383e81dc4533615c65ada7c84227328d17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 5 Aug 2010 21:23:32 +0200 Subject: [PATCH] Memory leaks: better handling of 'return strcmp(..' etc --- lib/checkmemoryleak.cpp | 10 +++++++++- test/testmemleak.cpp | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index e20f3f102..757ab3d10 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -28,6 +28,7 @@ #include #include #include +#include //--------------------------------------------------------------------------- @@ -1274,6 +1275,8 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list f; + for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->str() == ";") @@ -1282,6 +1285,11 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::liststr() == "(") + f.push(tok2->previous()); + else if (tok2->str() == ")") + f.pop(); + if (tok2->varId() == varid) { // Read data.. @@ -1289,7 +1297,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listnext(), "[")) { } - else + else if (f.empty() || !test_white_list(f.top()->str())) { use = true; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 4d2ae8aed..c911e2cde 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -456,6 +456,7 @@ private: // alloc; return; ASSERT_EQUALS(";;alloc;return;", getcode("char *s = new char[100]; return 0;", "s")); ASSERT_EQUALS(";;alloc;return;", getcode("char *s = new char[100]; return s[0];", "s")); + ASSERT_EQUALS(";;alloc;return;", getcode("char *s = new char[100]; return strcmp(s,x);", "s")); // lock/unlock.. ASSERT_EQUALS(";;alloc;", getcode("int a; __cppcheck_lock();", "")); @@ -1392,7 +1393,6 @@ private: //////////////////////////////////////////////// - void func3() { check("static void foo(const char *str)\n"