Improved detection of new operator in checkmemoryleak.cpp, fixed TODO unit test

This commit is contained in:
PKEuS 2015-11-18 20:33:39 +01:00
parent 0f9d90d2be
commit 6ee4cf80dc
2 changed files with 7 additions and 11 deletions

View File

@ -130,7 +130,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
if (! tok2->isName()) if (! tok2->isName())
return No; return No;
if (!Token::Match(tok2, "%type%|%name% ::|. %type%")) { if (!Token::Match(tok2, "%name% ::|. %type%")) {
// Does tok2 point on "malloc", "strdup" or "kmalloc".. // Does tok2 point on "malloc", "strdup" or "kmalloc"..
static const char * const mallocfunc[] = { static const char * const mallocfunc[] = {
"malloc", "malloc",
@ -150,15 +150,11 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
if (varid && Token::Match(tok2, "realloc ( %any% ,") && tok2->tokAt(2)->varId() != varid) if (varid && Token::Match(tok2, "realloc ( %any% ,") && tok2->tokAt(2)->varId() != varid)
return Malloc; return Malloc;
if (Token::Match(tok2, "new struct| %type% [;()]") || if (tokenizer->isCPP() && tok2->str() == "new") {
Token::Match(tok2, "new ( std :: nothrow ) struct| %type% [;()]") || if (tok2->astOperand1() && tok2->astOperand1()->str() == "[")
Token::Match(tok2, "new ( nothrow ) struct| %type% [;()]"))
return New;
if (Token::Match(tok2, "new struct| %type% *| [") ||
Token::Match(tok2, "new ( std :: nothrow ) struct| %type% *| [") ||
Token::Match(tok2, "new ( nothrow ) struct| %type% *| ["))
return NewArray; return NewArray;
return New;
}
if (Token::Match(tok2, "fopen|tmpfile|g_fopen (")) if (Token::Match(tok2, "fopen|tmpfile|g_fopen ("))
return File; return File;
@ -192,7 +188,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
} }
} }
while (Token::Match(tok2,"%type%|%name% ::|. %type%")) while (Token::Match(tok2,"%name% ::|. %type%"))
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
// User function // User function

View File

@ -2351,7 +2351,7 @@ private:
"{\n" "{\n"
" std::string *x = new std::string;\n" " std::string *x = new std::string;\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: x\n","", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: x\n", errout.str());
check("void f(void)\n" check("void f(void)\n"
"{\n" "{\n"