From 4e6800c4745b852558fe8804ae28fe85313cef9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 24 Jul 2011 16:08:29 +0200 Subject: [PATCH] Fixed #2921 (False positive: Memory leak with static pointer) --- lib/checkmemoryleak.cpp | 4 ++++ test/testmemleak.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 9a557af47..11e4c529c 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -467,6 +467,10 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok, { return No; } + if (Token::Match(tok, "static %type% * %varid% [;{}=]", varid)) + { + return No; + } if (Token::Match(tok, "[(,] %varid% [,)]", varid)) { return No; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 96a87665c..63321a733 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -244,6 +244,7 @@ private: TEST_CASE(allocfunc7); TEST_CASE(allocfunc8); TEST_CASE(allocfunc9); + TEST_CASE(allocfunc10); TEST_CASE(throw1); TEST_CASE(throw2); @@ -2528,6 +2529,19 @@ private: ASSERT_EQUALS("", errout.str()); } + void allocfunc10() + { + // Ticket #2921 - static pointer + check("char *getstr() {\n" + " static char *ret = malloc(100);\n" + " return ret;\n" + "}\n" + "\n" + "void foo() {\n" + " char *s = getstr();\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } void throw1() {