Memory leaks: better handling of 'return strcmp(..' etc

This commit is contained in:
Daniel Marjamäki 2010-08-05 21:23:32 +02:00
parent 81c058f2be
commit eedb5e383e
2 changed files with 10 additions and 2 deletions

View File

@ -28,6 +28,7 @@
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
//---------------------------------------------------------------------------
@ -1274,6 +1275,8 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
{
bool use = false;
std::stack<const Token *> f;
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == ";")
@ -1282,6 +1285,11 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
break;
}
if (tok2->str() == "(")
f.push(tok2->previous());
else if (tok2->str() == ")")
f.pop();
if (tok2->varId() == varid)
{
// Read data..
@ -1289,7 +1297,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
Token::simpleMatch(tok2->next(), "["))
{
}
else
else if (f.empty() || !test_white_list(f.top()->str()))
{
use = true;
}

View File

@ -456,6 +456,7 @@ private:
// alloc; return;
ASSERT_EQUALS(";;alloc;return;", getcode("char *s = new char[100]; return 0;", "s"));
ASSERT_EQUALS(";;alloc;return;", getcode("char *s = new char[100]; return s[0];", "s"));
ASSERT_EQUALS(";;alloc;return;", getcode("char *s = new char[100]; return strcmp(s,x);", "s"));
// lock/unlock..
ASSERT_EQUALS(";;alloc;", getcode("int a; __cppcheck_lock();", ""));
@ -1392,7 +1393,6 @@ private:
////////////////////////////////////////////////
void func3()
{
check("static void foo(const char *str)\n"