memory leak: detect memory leak (#6)

This commit is contained in:
Daniel Marjamäki 2009-01-17 07:55:40 +00:00
parent afe19dd0a2
commit f562f588fb
2 changed files with 25 additions and 1 deletions

View File

@ -972,6 +972,13 @@ void CheckMemoryLeakClass::simplifycode(Token *tok)
done = false;
}
// Replace "loop if return ;" with "if return ;"
if (Token::Match(tok2->next(), "loop if return"))
{
erase(tok2, tok2->tokAt(2));
done = false;
}
// Delete if block in "alloc ; if(!var) return ;"
if (Token::Match(tok2, "alloc ; if(!var) return ;"))
{

View File

@ -109,6 +109,7 @@ private:
TEST_CASE(switch1);
TEST_CASE(switch2);
TEST_CASE(switch3);
TEST_CASE(ret1);
TEST_CASE(ret2);
@ -835,7 +836,6 @@ private:
ASSERT_EQUALS(std::string(""), errout.str());
}
void switch2()
{
const std::string code("void f()\n"
@ -856,6 +856,23 @@ private:
ASSERT_EQUALS("[test.cpp:12]: Memory leak: str\n", errout.str());
}
void switch3()
{
check("void f()\n"
"{\n"
" char *str = new char[10];\n"
" while (abc)\n"
" {\n"
" switch (def)\n"
" {\n"
" default:\n"
" return;\n"
" }\n"
" }\n"
" delete [] str;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: Memory leak: str\n", errout.str());
}