Fixed #2492 (False positive: memory leak 'return &self->foo;')

This commit is contained in:
Daniel Marjamäki 2011-01-22 13:40:19 +01:00
parent 9551332321
commit f947955c63
2 changed files with 16 additions and 3 deletions

View File

@ -3101,7 +3101,8 @@ void CheckMemoryLeakStructMember::check()
else if (tok3->str() == "return") else if (tok3->str() == "return")
{ {
// Returning from function without deallocating struct member? // Returning from function without deallocating struct member?
if (!Token::Match(tok3, "return %varid% ;", structid)) if (!Token::Match(tok3, "return %varid% ;", structid) &&
!Token::Match(tok3, "return & %varid% .", structid))
{ {
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc); memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc);
} }

View File

@ -4206,7 +4206,8 @@ private:
TEST_CASE(goto_); TEST_CASE(goto_);
// Don't report errors if the struct is returned // Don't report errors if the struct is returned
TEST_CASE(ret); TEST_CASE(ret1);
TEST_CASE(ret2);
// assignments // assignments
TEST_CASE(assign); TEST_CASE(assign);
@ -4284,7 +4285,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void ret() void ret1()
{ {
check("static ABC * foo()\n" check("static ABC * foo()\n"
"{\n" "{\n"
@ -4301,6 +4302,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void ret2()
{
check("static ABC * foo()\n"
"{\n"
" struct ABC *abc = malloc(sizeof(struct ABC));\n"
" abc->a = malloc(10);\n"
" return &abc->self;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void assign() void assign()
{ {
check("static void foo()\n" check("static void foo()\n"