Fixed #1048 (False positive: exceptNew error with nothrow)

This commit is contained in:
Daniel Marjamäki 2009-12-05 22:05:59 +01:00
parent d1ebb83884
commit da4d03f6c4
2 changed files with 22 additions and 4 deletions

View File

@ -183,7 +183,7 @@ void CheckExceptionSafety::unsafeNew()
localVars.insert(tok->varId()); localVars.insert(tok->varId());
} }
if (Token::Match(tok, "; %var% = new")) if (Token::Match(tok, "; %var% = new %type%"))
{ {
if (!varname.empty()) if (!varname.empty())
{ {
@ -221,7 +221,7 @@ void CheckExceptionSafety::realloc()
break; break;
// reallocating.. // reallocating..
if (!Token::Match(tok, "%var% ; %var% = new")) if (!Token::Match(tok, "%var% ; %var% = new %type%"))
continue; continue;
// variable id of deallocated pointer.. // variable id of deallocated pointer..

View File

@ -90,6 +90,13 @@ private:
" A *a2 = new A;\n" " A *a2 = new A;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Upon exception there is memory leak: a1\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (style) Upon exception there is memory leak: a1\n", errout.str());
check("void a()\n"
"{\n"
" A *a1 = new A;\n"
" A *a2 = new (std::nothrow) A;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void realloc() void realloc()
@ -100,11 +107,22 @@ private:
" void a()\n" " void a()\n"
" {\n" " {\n"
" delete p;\n" " delete p;\n"
" p = new[123];\n" " p = new int[123];\n"
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Upon exception p becomes a dead pointer\n", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (style) Upon exception p becomes a dead pointer\n", errout.str());
check("class A\n"
"{\n"
" int *p;\n"
" void a()\n"
" {\n"
" delete p;\n"
" p = new (std::nothrow) int[123];\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("class A\n" check("class A\n"
"{\n" "{\n"
" int *p;\n" " int *p;\n"
@ -112,7 +130,7 @@ private:
" {\n" " {\n"
" try {\n" " try {\n"
" delete p;\n" " delete p;\n"
" p = new[123];\n" " p = new int[123];\n"
" } catch (...) { p = 0; }\n" " } catch (...) { p = 0; }\n"
" }\n" " }\n"
"}\n"); "}\n");