From e13aa0dbc359b682581b236ae966c550e144d6d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 29 Dec 2013 09:51:29 +0100 Subject: [PATCH] Fixed #5275 (FP mismatchAllocDealloc: user defined opendir()/closedir() on non-POSIX system) --- lib/checkmemoryleak.cpp | 7 +++++-- test/testmemleak.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 3c4e0e412..600167c17 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -179,8 +179,11 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, if (Token::simpleMatch(tok2, "popen (")) return Pipe; - if (Token::Match(tok2, "opendir|fdopendir (")) - return Dir; + if (settings1->standards.posix) + { + if (Token::Match(tok2, "opendir|fdopendir (")) + return Dir; + } // Does tok2 point on "g_malloc", "g_strdup", .. const int alloctype = settings1->library.alloc(tok2->str()); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index dc2835bb4..bb2487b34 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -204,6 +204,7 @@ private: TEST_CASE(mismatch4); TEST_CASE(mismatch5); TEST_CASE(mismatch6); + TEST_CASE(mismatch7); // opendir()/closedir() on non-POSIX TEST_CASE(mismatchSize); @@ -1503,6 +1504,33 @@ private: ASSERT_EQUALS("", errout.str()); } + void mismatch7() { + Settings settings; + settings.standards.posix = false; + const char mycode1[]= "DIR *opendir(const char *name);\n" + "void closedir(DIR *dir) {\n" + " free(dir);\n" + "}\n" + "\n" + "void f(const char *dir) {\n" + " DIR *dirp;\n" + " if ((dirp = opendir(dir)) == NULL) return 0;\n" + " closedir(dirp);\n" + "}\n"; + check(mycode1, &settings); + ASSERT_EQUALS("", errout.str()); + settings.standards.posix = true; + check(mycode1, &settings); + ASSERT_EQUALS("", errout.str()); + const char mycode2[]= "void f(const char *dir) {\n" + " DIR *dirp;\n" + " if ((dirp = opendir(dir)) == NULL) return 0;\n" + " free(dirp);\n" + "}\n"; + check(mycode2, &settings); + ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: dirp\n", errout.str()); + } + void mismatchSize() { check("void f(char *buf)\n" "{\n"