Fixed ticket #360 (Teach about new(std::nothrow) form)

http://apps.sourceforge.net/trac/cppcheck/ticket/360
This commit is contained in:
Slava Semushin 2009-06-05 11:03:48 +07:00
parent 58781c761c
commit 21e0639443
2 changed files with 47 additions and 2 deletions

View File

@ -103,10 +103,14 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType(const To
return gMalloc; return gMalloc;
} }
if (Token::Match(tok2, "new %type% [;()]")) if (Token::Match(tok2, "new %type% [;()]") ||
Token::Match(tok2, "new ( std :: nothrow ) %type% [;()]") ||
Token::Match(tok2, "new ( nothrow ) %type% [;()]"))
return New; return New;
if (Token::Match(tok2, "new %type% [")) if (Token::Match(tok2, "new %type% [") ||
Token::Match(tok2, "new ( std :: nothrow ) %type% [") ||
Token::Match(tok2, "new ( nothrow ) %type% ["))
return NewArray; return NewArray;
if (Token::Match(tok2, "fopen|tmpfile (")) if (Token::Match(tok2, "fopen|tmpfile ("))

View File

@ -69,6 +69,7 @@ private:
TEST_CASE(simple8); TEST_CASE(simple8);
TEST_CASE(simple9); // Bug 2435468 - member function "free" TEST_CASE(simple9); // Bug 2435468 - member function "free"
TEST_CASE(simple10); // fclose in a if condition TEST_CASE(simple10); // fclose in a if condition
TEST_CASE(new_nothrow);
TEST_CASE(alloc_alloc_1); TEST_CASE(alloc_alloc_1);
@ -340,8 +341,48 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void new_nothrow()
{
check("void f()\n"
"{\n"
" int *p = new(std::nothrow) int;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
check("void f()\n"
"{\n"
" using std::nothrow;\n"
" int *p = new(nothrow) int;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
check("void f()\n"
"{\n"
" int *p = new(std::nothrow) int[10];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
check("void f()\n"
"{\n"
" using namespace std;\n"
" int *p = new(nothrow) int[10];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
check("void f()\n"
"{\n"
" int *p = new(std::nothrow) int;\n"
" delete [] p;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: p\n", errout.str());
check("void f()\n"
"{\n"
" int *p = new(std::nothrow) int[10];\n"
" delete p;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: p\n", errout.str());
}
void alloc_alloc_1() void alloc_alloc_1()
{ {