From 269a4419f088747b22721de4790e20a37ee85a21 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Wed, 10 Dec 2014 22:21:54 +0100 Subject: [PATCH] Fixed false positives about strdupa() / strndupa() memleak strdupa() / strndupa() allocates memory on the stack using alloca(). This memory is freed automatically once the current function is left. --- cfg/posix.cfg | 4 ++-- test/testmemleak.cpp | 7 +++++++ test/testother.cpp | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cfg/posix.cfg b/cfg/posix.cfg index df4cdcb6c..c1064ba86 100644 --- a/cfg/posix.cfg +++ b/cfg/posix.cfg @@ -26,6 +26,7 @@ + false @@ -43,6 +44,7 @@ + false @@ -342,8 +344,6 @@ free strdup strndup - strdupa - strndupa wcsdup diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index d050d6af8..0aaaeb96b 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -4277,6 +4277,13 @@ private: "}", &settings); ASSERT_EQUALS("[test.cpp:3]: (error) Resource leak: f\n", errout.str()); + // strdupa allocates on the stack, no free() needed + check("void x()\n" + "{\n" + " char *s = strdupa(\"Test\");\n" + "}", &settings); + ASSERT_EQUALS("", errout.str()); + LOAD_LIB_2(settings.library, "gtk.cfg"); check("void f(char *a) {\n" diff --git a/test/testother.cpp b/test/testother.cpp index 72631d0b3..fe8f4e6b6 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -172,6 +172,7 @@ private: TEST_CASE(integerOverflow); // #5895 TEST_CASE(testReturnIgnoredReturnValue); + TEST_CASE(testReturnIgnoredReturnValuePosix); } void check(const char code[], const char *filename = nullptr, bool experimental = false, bool inconclusive = true, bool posix = false, bool runSimpleChecks=true, Settings* settings = 0) { @@ -6366,6 +6367,23 @@ private: "}"); ASSERT_EQUALS("", errout.str()); } + + void testReturnIgnoredReturnValuePosix() { + Settings settings_posix; + LOAD_LIB_2(settings_posix.library, "posix.cfg"); + + check("void f() {\n" + " strdupa(\"test\");\n" + "}", + "test.cpp", + false, // experimental + false, // inconclusive + true, // posix + false, // runSimpleChecks + &settings_posix + ); + ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function strdupa() is not used.\n", errout.str()); + } }; REGISTER_TEST(TestOther)