diff --git a/lib/checkexceptionsafety.cpp b/lib/checkexceptionsafety.cpp index 6dba9914b..70c3fc30d 100644 --- a/lib/checkexceptionsafety.cpp +++ b/lib/checkexceptionsafety.cpp @@ -183,7 +183,7 @@ void CheckExceptionSafety::unsafeNew() localVars.insert(tok->varId()); } - if (Token::Match(tok, "; %var% = new")) + if (Token::Match(tok, "; %var% = new %type%")) { if (!varname.empty()) { @@ -221,7 +221,7 @@ void CheckExceptionSafety::realloc() break; // reallocating.. - if (!Token::Match(tok, "%var% ; %var% = new")) + if (!Token::Match(tok, "%var% ; %var% = new %type%")) continue; // variable id of deallocated pointer.. diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index 15465067d..6eddbc043 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -90,6 +90,13 @@ private: " A *a2 = new A;\n" "}\n"); 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() @@ -100,11 +107,22 @@ private: " void a()\n" " {\n" " delete p;\n" - " p = new[123];\n" + " p = new int[123];\n" " }\n" "}\n"); 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" "{\n" " int *p;\n" @@ -112,7 +130,7 @@ private: " {\n" " try {\n" " delete p;\n" - " p = new[123];\n" + " p = new int[123];\n" " } catch (...) { p = 0; }\n" " }\n" "}\n");