Fixed #746 (False positive, Memory leak when goto is used)

This commit is contained in:
Daniel Marjamäki 2009-09-28 22:58:06 +02:00
parent 1427f0a2c7
commit 311f6dc92e
2 changed files with 24 additions and 1 deletions

View File

@ -2116,10 +2116,17 @@ void CheckMemoryLeakStructMember::check()
ignoredFunctions.insert("while");
ignoredFunctions.insert("malloc");
unsigned int indentlevel1 = 0;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (tok->str() == "{")
++indentlevel1;
else if (tok->str() == "}")
--indentlevel1;
// Locate struct variables..
if (Token::Match(tok, "struct|;|{|} %type% * %var% [=;]"))
if (indentlevel1 > 0 && Token::Match(tok, "struct|;|{|} %type% * %var% [=;]"))
{
const Token * const vartok = tok->tokAt(3);
if (vartok->varId() == 0)

View File

@ -2733,6 +2733,9 @@ private:
// Handle if-else
TEST_CASE(ifelse);
// struct variable is a global variable
TEST_CASE(globalvar);
}
void err()
@ -2896,6 +2899,19 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
}
void globalvar()
{
check("struct ABC *abc;\n"
"\n"
"static void foo()\n"
"{\n"
" abc = malloc(sizeof(struct ABC));\n"
" abc->a = malloc(10);\n"
" return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};