Skip to content

Commit 68202d8

Browse files
committed
Extra check for auto_ptr new[]
This fixes cases like this: auto_ptr<foo> bar(new foo[10]); which previously did not work correctly.
1 parent 2a46c63 commit 68202d8

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lib/checkstl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,10 @@ void CheckStl::checkAutoPointer()
11011101
while (tok2) {
11021102
if (Token::Match(tok2, "> %var%")) {
11031103
const Token *tok3 = tok2->next()->next();
1104+
if (Token::Match(tok3, "( new %type% [")) {
1105+
autoPointerArrayError(tok2->next());
1106+
break;
1107+
}
11041108
while (tok3 && tok3->str() != ";") {
11051109
tok3 = tok3->next();
11061110
}

test/teststl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,12 @@ class TestStl : public TestFixture {
14331433
"}\n");
14341434
ASSERT_EQUALS("[test.cpp:3]: (error) Object pointed by an 'auto_ptr' is destroyed using operator 'delete'. You should not use 'auto_ptr' for pointers obtained with operator 'new[]'.\n", errout.str());
14351435

1436+
check("void f() \n"
1437+
"{\n"
1438+
" auto_ptr<T> p2( new T[5] );\n"
1439+
"}\n");
1440+
ASSERT_EQUALS("[test.cpp:3]: (error) Object pointed by an 'auto_ptr' is destroyed using operator 'delete'. You should not use 'auto_ptr' for pointers obtained with operator 'new[]'.\n", errout.str());
1441+
14361442
check("void f() \n"
14371443
"{\n"
14381444
" auto_ptr<T> p2;\n"

0 commit comments

Comments
 (0)