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.
This commit is contained in:
Thomas Jarosch 2014-12-10 22:21:54 +01:00
parent def3491829
commit 269a4419f0
3 changed files with 27 additions and 2 deletions

View File

@ -26,6 +26,7 @@
</arg>
</function>
<function name="strdupa">
<use-retval/>
<noreturn>false</noreturn>
<arg nr="1">
<not-null/>
@ -43,6 +44,7 @@
</arg>
</function>
<function name="strndupa">
<use-retval/>
<noreturn>false</noreturn>
<arg nr="1">
<not-null/>
@ -342,8 +344,6 @@
<dealloc>free</dealloc>
<alloc init="true">strdup</alloc>
<alloc init="true">strndup</alloc>
<alloc init="true">strdupa</alloc>
<alloc init="true">strndupa</alloc>
<alloc init="true">wcsdup</alloc>
</memory>
<resource>

View File

@ -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"

View File

@ -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)