Fixed #3904 (false positive memory leak with linked list)

This commit is contained in:
Daniel Marjamäki 2012-06-23 20:15:58 +02:00
parent 162a430354
commit 72d24ab4b4
2 changed files with 20 additions and 0 deletions

View File

@ -2641,6 +2641,8 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var
// struct assignment.. // struct assignment..
else if (Token::Match(tok3, "= %varid% ;", structid)) { else if (Token::Match(tok3, "= %varid% ;", structid)) {
break; break;
} else if (Token::Match(tok3, "= %var% . %varid% ;", structmemberid)) {
break;
} }
// goto isn't handled well.. bail out even though there might be leaks // goto isn't handled well.. bail out even though there might be leaks

View File

@ -4960,6 +4960,9 @@ private:
// Handle if-else // Handle if-else
TEST_CASE(ifelse); TEST_CASE(ifelse);
// Linked list
TEST_CASE(linkedlist);
// struct variable is a global variable // struct variable is a global variable
TEST_CASE(globalvar); TEST_CASE(globalvar);
@ -5180,6 +5183,21 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void linkedlist() {
check("static void foo() {\n"
" struct ABC *abc = malloc(sizeof(struct ABC));\n"
" abc->next = malloc(sizeof(struct ABC));\n"
" abc->next->next = NULL;\n"
"\n"
" while (abc) {\n"
" struct ABC *next = abc->next;\n"
" free(abc);\n"
" abc = next;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void globalvar() { void globalvar() {
check("struct ABC *abc;\n" check("struct ABC *abc;\n"
"\n" "\n"