Fix ticket #371 (Resource leak when exit() and if() uses together)

http://apps.sourceforge.net/trac/cppcheck/ticket/371
This commit is contained in:
Reijo Tomperi 2009-06-07 09:55:20 +03:00
parent 5747133fa8
commit 9bac4aca75
2 changed files with 25 additions and 4 deletions

View File

@ -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..

View File

@ -2234,6 +2234,14 @@ private:
" 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()