From 68202d8ffbe719e3f8b4ab14aa3e49cb8b48475d Mon Sep 17 00:00:00 2001 From: Richard Quirk Date: Sat, 5 Nov 2011 15:40:50 +0100 Subject: [PATCH] Extra check for auto_ptr new[] This fixes cases like this: auto_ptr bar(new foo[10]); which previously did not work correctly. --- lib/checkstl.cpp | 4 ++++ test/teststl.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 55880485c..37e9d5c20 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1101,6 +1101,10 @@ void CheckStl::checkAutoPointer() while (tok2) { if (Token::Match(tok2, "> %var%")) { const Token *tok3 = tok2->next()->next(); + if (Token::Match(tok3, "( new %type% [")) { + autoPointerArrayError(tok2->next()); + break; + } while (tok3 && tok3->str() != ";") { tok3 = tok3->next(); } diff --git a/test/teststl.cpp b/test/teststl.cpp index 8edd73225..aa28e834a 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1433,6 +1433,12 @@ private: "}\n"); 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()); + check("void f() \n" + "{\n" + " auto_ptr p2( new T[5] );\n" + "}\n"); + 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()); + check("void f() \n" "{\n" " auto_ptr p2;\n"