From 9beb73824b0a75b6e9b605000ee25392aadaa0b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 6 Jul 2009 12:20:13 +0200 Subject: [PATCH] Fixed #446 (memory leak false positive when variable is static) --- src/checkmemoryleak.cpp | 7 +++++++ test/testmemleak.cpp | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 2b120b1b3..ed5ff35a9 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -1654,6 +1654,13 @@ void CheckMemoryLeakInFunction::check() if (sz < 1) sz = 1; + if (!Token::Match(tok, "[{};] %type%")) + continue; + + // Don't check static variables + if (tok->next()->str() == "static") + continue; + if (Token::Match(tok, "[{};] %type% * const| %var% [;=]")) { const int varname_tok = (tok->tokAt(3)->str() != "const" ? 3 : 4); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 7775210c6..03712ddcd 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -131,6 +131,8 @@ private: TEST_CASE(simple11); TEST_CASE(new_nothrow); + TEST_CASE(staticvar); + TEST_CASE(alloc_alloc_1); TEST_CASE(use1); @@ -478,6 +480,21 @@ private: ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: p\n", errout.str()); } + + void staticvar() + { + check("int f()\n" + "{\n" + " static char *s = 0;\n" + " free(s);\n" + " s = malloc(100);\n" + " return 123;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + + + void alloc_alloc_1() { check("void foo()\n"