From cf0c666d793b4a4f7bffa0f7ce78c362156e0144 Mon Sep 17 00:00:00 2001 From: Steve Duan Date: Sat, 28 Sep 2013 11:50:45 +0200 Subject: [PATCH] Fixed #5041 (Improve check: support 'auto' for 'Iterator used after element has been erased') --- lib/checkstl.cpp | 2 +- test/teststl.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 85130e6af..dd3c40ae8 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -67,7 +67,7 @@ void CheckStl::iterators() const Variable* var = symbolDatabase->getVariableFromVarId(iteratorId); // Check that its an iterator - if (!var || !var->isLocal() || !Token::Match(var->typeEndToken(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) + if (!var || !var->isLocal() || !Token::Match(var->typeEndToken(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator|auto")) continue; if (var->type()) { // If it is defined, ensure that it is defined like an iterator diff --git a/test/teststl.cpp b/test/teststl.cpp index ca0162a79..559e4a47a 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -125,6 +125,7 @@ private: TEST_CASE(stabilityOfChecks); // #4684 cppcheck crash in template function call TEST_CASE(dereferenceInvalidIterator); + TEST_CASE(dereference_auto); } @@ -490,6 +491,16 @@ private: ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str()); } + void dereference_auto() { + check("void f()\n" + "{\n" + " std::vector ints;\n" + " auto iter = ints.begin() + 2;\n" + " ints.erase(iter);\n" + " std::cout << (*iter) << std::endl;\n" + "}"); + ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:5]: (error) Iterator 'iter' used after element has been erased.\n", errout.str()); + } void STLSize() { check("void foo()\n"