Fixed #1460 (false positive: memory leak when using throw no_memory)

This commit is contained in:
Daniel Marjamäki 2010-03-13 17:11:48 +01:00
parent 772aa95c8a
commit 65ae37de2f
2 changed files with 18 additions and 17 deletions

View File

@ -953,6 +953,11 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
{ {
dep = true; dep = true;
} }
if (parlevel > 0 && Token::Match(tok2, "! %varid%", varid))
{
dep = true;
break;
}
} }
if (Token::Match(tok, "if ( ! %varid% &&", varid)) if (Token::Match(tok, "if ( ! %varid% &&", varid))
@ -969,6 +974,8 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
{ {
addtoken((dep ? "ifv" : "if")); addtoken((dep ? "ifv" : "if"));
} }
tok = tok->next()->link();
} }
} }
} }
@ -1499,6 +1506,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
done = false; done = false;
} }
// Reduce "ifv return;" => "if return use;"
if (Token::simpleMatch(tok2, "ifv return ;"))
{
tok2->str("if");
tok2->next()->insertToken("use");
done = false;
}
// Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else.. // Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else..
if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else")) if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else"))
{ {

View File

@ -254,7 +254,6 @@ private:
TEST_CASE(ifelse9); TEST_CASE(ifelse9);
TEST_CASE(ifelse10); TEST_CASE(ifelse10);
TEST_CASE(if1);
TEST_CASE(if4); TEST_CASE(if4);
TEST_CASE(if7); // Bug 2401436 TEST_CASE(if7); // Bug 2401436
TEST_CASE(if8); // Bug 2458532 TEST_CASE(if8); // Bug 2458532
@ -463,6 +462,7 @@ private:
ASSERT_EQUALS(";;if(!var){}else{}", getcode("char *s; if (!s) { } else { }", "s")); ASSERT_EQUALS(";;if(!var){}else{}", getcode("char *s; if (!s) { } else { }", "s"));
ASSERT_EQUALS(";;if{}", getcode("char *s; if (a && s) { }", "s")); ASSERT_EQUALS(";;if{}", getcode("char *s; if (a && s) { }", "s"));
ASSERT_EQUALS(";;if(!var){}", getcode("char *s; if (a && !s) { }", "s")); ASSERT_EQUALS(";;if(!var){}", getcode("char *s; if (a && !s) { }", "s"));
ASSERT_EQUALS(";;ifv{}", getcode("char *s; if (foo(!s)) { }", "s"));
ASSERT_EQUALS(";;;if{dealloc;};if{dealloc;return;}assign;returnuse;", getcode("char *buf, *tmp; tmp = realloc(buf, 40); if (!(tmp)) { free(buf); return; } buf = tmp; return buf;", "buf")); ASSERT_EQUALS(";;;if{dealloc;};if{dealloc;return;}assign;returnuse;", getcode("char *buf, *tmp; tmp = realloc(buf, 40); if (!(tmp)) { free(buf); return; } buf = tmp; return buf;", "buf"));
// switch.. // switch..
@ -667,6 +667,7 @@ private:
// if(var) // if(var)
ASSERT_EQUALS("; alloc ; return use ;", simplifycode("; alloc ; return use ;")); ASSERT_EQUALS("; alloc ; return use ;", simplifycode("; alloc ; return use ;"));
ASSERT_EQUALS("; alloc ; return use ;", simplifycode("; alloc ; ifv return ; return use ;"));
// switch.. // switch..
ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode(";alloc;switch{case;break;};dealloc;")); ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode(";alloc;switch{case;break;};dealloc;"));
@ -1019,21 +1020,6 @@ private:
void if1()
{
check("void f()\n"
"{\n"
" struct abc *p = new abc;\n"
" p->a = new char[100];\n"
" if ( ! p->a )\n"
" return;\n"
" foo(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: p\n", errout.str());
}
void if4() void if4()
{ {
check("void f()\n" check("void f()\n"
@ -1125,7 +1111,7 @@ private:
" }\n" " }\n"
" delete [] x;\n" " delete [] x;\n"
"}\n", true); "}\n", true);
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: x\n", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: x\n", errout.str());
} }