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;