File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed
Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -183,7 +183,7 @@ void CheckExceptionSafety::unsafeNew()
183183 localVars.insert (tok->varId ());
184184 }
185185
186- if (Token::Match (tok, " ; %var% = new" ))
186+ if (Token::Match (tok, " ; %var% = new %type% " ))
187187 {
188188 if (!varname.empty ())
189189 {
@@ -221,7 +221,7 @@ void CheckExceptionSafety::realloc()
221221 break ;
222222
223223 // reallocating..
224- if (!Token::Match (tok, " %var% ; %var% = new" ))
224+ if (!Token::Match (tok, " %var% ; %var% = new %type% " ))
225225 continue ;
226226
227227 // variable id of deallocated pointer..
Original file line number Diff line number Diff line change @@ -90,6 +90,13 @@ class TestExceptionSafety : public TestFixture
9090 " A *a2 = new A;\n "
9191 " }\n " );
9292 ASSERT_EQUALS (" [test.cpp:4]: (style) Upon exception there is memory leak: a1\n " , errout.str ());
93+
94+ check (" void a()\n "
95+ " {\n "
96+ " A *a1 = new A;\n "
97+ " A *a2 = new (std::nothrow) A;\n "
98+ " }\n " );
99+ ASSERT_EQUALS (" " , errout.str ());
93100 }
94101
95102 void realloc ()
@@ -100,19 +107,30 @@ class TestExceptionSafety : public TestFixture
100107 " void a()\n "
101108 " {\n "
102109 " delete p;\n "
103- " p = new[123];\n "
110+ " p = new int [123];\n "
104111 " }\n "
105112 " }\n " );
106113 ASSERT_EQUALS (" [test.cpp:7]: (style) Upon exception p becomes a dead pointer\n " , errout.str ());
107114
115+ check (" class A\n "
116+ " {\n "
117+ " int *p;\n "
118+ " void a()\n "
119+ " {\n "
120+ " delete p;\n "
121+ " p = new (std::nothrow) int[123];\n "
122+ " }\n "
123+ " }\n " );
124+ ASSERT_EQUALS (" " , errout.str ());
125+
108126 check (" class A\n "
109127 " {\n "
110128 " int *p;\n "
111129 " void a()\n "
112130 " {\n "
113131 " try {\n "
114132 " delete p;\n "
115- " p = new[123];\n "
133+ " p = new int [123];\n "
116134 " } catch (...) { p = 0; }\n "
117135 " }\n "
118136 " }\n " );
You can’t perform that action at this time.
0 commit comments