From fa305d70bc5d634f8e6bfe4e2d57c6f489685b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 26 Jan 2010 20:10:52 +0100 Subject: [PATCH] Fixed #1302 (False positive: Confusion between POSIX open() and class member) --- lib/checkmemoryleak.cpp | 5 +++++ test/testmemleak.cpp | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 91516f2ed..bce233230 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -152,7 +152,12 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, return File; if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp (")) + { + // is there a user function with this name? + if (tokenizer && Token::findmatch(tokenizer->tokens(), ("%type% *|&| " + tok2->str()).c_str())) + return No; return Fd; + } if (Token::simpleMatch(tok2, "popen (")) return Pipe; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index d7efc5a3a..85f4b22d6 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -122,6 +122,7 @@ private: void run() { TEST_CASE(testFunctionReturnType); + TEST_CASE(open); } CheckMemoryLeak::AllocType functionReturnType(const char code[]) @@ -164,6 +165,27 @@ private: ASSERT_EQUALS(CheckMemoryLeak::NewArray, functionReturnType(code)); } } + + void open() + { + const char code[] = "class A {\n" + " static int open() {\n" + " return 1;\n" + " }\n" + "\n" + " A() {\n" + " int ret = open();\n" + " }\n" + "};\n"; + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + // there is no allocation + const Token *tok = Token::findmatch(tokenizer.tokens(), "ret ="); + CheckMemoryLeak check(&tokenizer, 0); + ASSERT_EQUALS(CheckMemoryLeak::No, check.getAllocationType(tok->tokAt(2), 1)); + } }; static TestMemleak testMemleak;