From 9c60391375ab028f7cbed01db3011bd41987cf04 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Tue, 19 May 2009 23:29:10 +0300 Subject: [PATCH] Apply patch #320 from php-coderrr ([PATCH] Determine memory leaks after strndup() usage) http://apps.sourceforge.net/trac/cppcheck/ticket/320 --- src/checkmemoryleak.cpp | 2 ++ test/testmemleak.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index e71c6541f..97670a6b8 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -57,6 +57,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType(const To // * var = (char *)malloc(10); // * var = new char[10]; // * var = strdup("hello"); + // * var = strndup("hello", 3); if (tok2 && tok2->str() == "(") { while (tok2 && tok2->str() != ")") @@ -72,6 +73,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType(const To const char *mallocfunc[] = {"malloc", "calloc", "strdup", + "strndup", "kmalloc", "kzalloc", "kcalloc", diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 32352b12f..8cda355e1 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -213,6 +213,8 @@ private: TEST_CASE(exit1); TEST_CASE(exit2); TEST_CASE(stdstring); + + TEST_CASE(strndup_function); } @@ -2165,6 +2167,16 @@ private: TODO_ASSERT_EQUALS(std::string("[test.cpp:5]: (error) Memory leak: out\n"), err); } + + void strndup_function() + { + check("void f()\n" + "{\n" + " char *out = strndup(\"text\", 3);\n" + "}\n"); + ASSERT_EQUALS(std::string("[test.cpp:4]: (error) Memory leak: out\n"), errout.str()); + } + }; REGISTER_TEST(TestMemleak)