Skip to content

Commit

Permalink
Fix ticket #371 (Resource leak when exit() and if() uses together)
Browse files Browse the repository at this point in the history
  • Loading branch information
aggro80 committed Jun 7, 2009
1 parent 5747133 commit 9bac4ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/checkmemoryleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,13 +809,26 @@ void CheckMemoryLeakClass::simplifycode(Token *tok, bool &all)
continue;

// Found an "exit".. try to remove everything before it
const Token *tokEnd = tok2->next();
while (tok2->previous() && !Token::Match(tok2->previous(), "[{}]"))
tok2 = tok2->previous();
if (tok2->previous())
Token *tokEnd = tok2->next();
int indentlevel = 0;
while (tok2->previous())
{
tok2 = tok2->previous();
if (tok2->str() == "}")
{
indentlevel--;
}
else if (tok2->str() == "{")
{
if (indentlevel == 0)
break;

indentlevel++;
}
}

Token::eraseTokens(tok2, tokEnd);
tok2 = tokEnd;
}

// reduce the code..
Expand Down
8 changes: 8 additions & 0 deletions test/testmemleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,14 @@ class TestMemleak : public TestFixture
" exit(0);\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f()\n"
"{\n"
" char *out = new char[100];\n"
" if( out ) {}\n"
" exit(0);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void stdstring()
Expand Down

0 comments on commit 9bac4ac

Please sign in to comment.