Fixed #2848 (False positive: Memory leak when assigning and freeing from another struct)

This commit is contained in:
Daniel Marjamäki 2011-07-21 13:33:20 +02:00
parent 9f22d2fc6b
commit d34a778848
2 changed files with 15 additions and 4 deletions

View File

@ -3185,7 +3185,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok
--parlevel; --parlevel;
} }
if (Token::Match(tok4, "[(,] %varid% [,)]", structid)) if (Token::Match(tok4, "[(,] &| %varid% [,)]", structid))
{ {
/** @todo check if the function deallocates the memory */ /** @todo check if the function deallocates the memory */
deallocated = true; deallocated = true;

View File

@ -4918,8 +4918,8 @@ private:
// Failed allocation // Failed allocation
TEST_CASE(failedAllocation); TEST_CASE(failedAllocation);
// Deallocating in a function TEST_CASE(function1); // Deallocating in function
TEST_CASE(function); TEST_CASE(function2); // #2848: Taking address in function
// Handle if-else // Handle if-else
TEST_CASE(ifelse); TEST_CASE(ifelse);
@ -5073,7 +5073,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void function() void function1()
{ {
// Not found function => assume that the function may deallocate // Not found function => assume that the function may deallocate
check("static void foo()\n" check("static void foo()\n"
@ -5093,6 +5093,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
// #2848: Taking address in function 'assign'
void function2()
{
check("void f() {\n"
" A a = { 0 };\n"
" a.foo = (char *) malloc(10);\n"
" assign(&a);\n"
"}\n", "test.c");
ASSERT_EQUALS("", errout.str());
}
void ifelse() void ifelse()
{ {
check("static void foo()\n" check("static void foo()\n"