Fixed #446 (memory leak false positive when variable is static)

This commit is contained in:
Daniel Marjamäki 2009-07-06 12:20:13 +02:00
parent a6ac747830
commit 9beb73824b
2 changed files with 24 additions and 0 deletions

View File

@ -1654,6 +1654,13 @@ void CheckMemoryLeakInFunction::check()
if (sz < 1)
sz = 1;
if (!Token::Match(tok, "[{};] %type%"))
continue;
// Don't check static variables
if (tok->next()->str() == "static")
continue;
if (Token::Match(tok, "[{};] %type% * const| %var% [;=]"))
{
const int varname_tok = (tok->tokAt(3)->str() != "const" ? 3 : 4);

View File

@ -131,6 +131,8 @@ private:
TEST_CASE(simple11);
TEST_CASE(new_nothrow);
TEST_CASE(staticvar);
TEST_CASE(alloc_alloc_1);
TEST_CASE(use1);
@ -478,6 +480,21 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: p\n", errout.str());
}
void staticvar()
{
check("int f()\n"
"{\n"
" static char *s = 0;\n"
" free(s);\n"
" s = malloc(100);\n"
" return 123;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void alloc_alloc_1()
{
check("void foo()\n"