From 1cf2e36dd52a2d7d26671479da7e109b8b22e990 Mon Sep 17 00:00:00 2001 From: Pierre Schweitzer Date: Sun, 1 Jun 2014 00:00:49 +0200 Subject: [PATCH] Add a new test for OpenFile function from windows.cfg. It is here as a reminder for a TODO. When used with OF_EXIST flag, the function OpenFile doesn't return a handle and thus, doesn't leak any resource. As cppcheck doesn't support such feature yet, it's added a TODO not to be forgotten for later fix when cppcheck supports this. Also added a naive check, just to ensure the OpenFile check doesn't get broken when adding support for OF_EXIST. --- test/testmemleak.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index d0ef25b6c..e492d1fb2 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -6423,3 +6423,59 @@ private: } }; static TestMemleakGLib testMemleakGLib; + + + + + +class TestMemleakWindows : public TestFixture { +public: + TestMemleakWindows() : TestFixture("TestMemleakWindows") { + } + +private: + Settings settings; + + void check(const char code[]) { + // Clear the error buffer.. + errout.str(""); + + // Preprocess... + Preprocessor preprocessor(&settings, this); + std::istringstream istrpreproc(code); + std::map actual; + preprocessor.preprocess(istrpreproc, actual, "test.c"); + + // Tokenize.. + Tokenizer tokenizer(&settings, this); + std::istringstream istr(actual[""]); + tokenizer.tokenize(istr, "test.c"); + tokenizer.simplifyTokenList2(); + + // Check for memory leaks.. + CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, this); + checkMemoryLeak.checkReallocUsage(); + checkMemoryLeak.check(); + } + + void run() { + LOAD_LIB_2(settings.library, "windows.cfg"); + + TEST_CASE(openfileNoLeak); + } + + void openfileNoLeak() { + check("void f() {" + " OFSTRUCT OfStr;" + " int hFile = OpenFile(\"file\", &OfStr, 0);" + "}"); + ASSERT_EQUALS("[test.c:1]: (error) Resource leak: hFile\n", errout.str()); + + check("void f() {" + " OFSTRUCT OfStr;" + " int hFile = OpenFile(\"file\", &OfStr, OF_EXIST);" + "}"); + TODO_ASSERT_EQUALS("", "[test.c:1]: (error) Resource leak: hFile\n", errout.str()); + } +}; +static TestMemleakWindows testMemleakWindows;