From 8cb3b13e564b39d4ec27665096bc7af16e991b36 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 12 Apr 2014 19:54:31 +0200 Subject: [PATCH] Support "else if" and do-loop in CheckStl::checkDereferenceInvalidIterator() --- lib/checkstl.cpp | 6 +++++- test/teststl.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 1ba76ee83..001d6bc28 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1471,10 +1471,14 @@ void CheckStl::checkDereferenceInvalidIterator() // be an iterator that is dereferenced before being checked for validity. const std::list& scopeList = _tokenizer->getSymbolDatabase()->scopeList; for (std::list::const_iterator i = scopeList.begin(); i != scopeList.end(); ++i) { - if (i->type == Scope::eIf || i->type == Scope::eWhile || i->type == Scope::eFor) { + if (i->type == Scope::eIf || i->type == Scope::eElseIf || i->type == Scope::eDo || i->type == Scope::eWhile || i->type == Scope::eFor) { const Token* const tok = i->classDef; const Token* startOfCondition = tok->next(); + if (i->type == Scope::eElseIf) + startOfCondition = startOfCondition->next(); + else if (i->type == Scope::eDo) + startOfCondition = startOfCondition->link()->tokAt(2); const Token* endOfCondition = startOfCondition->link(); if (!endOfCondition) continue; diff --git a/test/teststl.cpp b/test/teststl.cpp index 6c898d443..2e05d11de 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -2343,6 +2343,14 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str()); + check("void foo(std::string::iterator& i) {\n" + " if(foo) { bar(); }\n" + " else if (std::isalpha(*i) && i != str.end()) {\n" + " std::cout << *i;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Possible dereference of an invalid iterator: i\n", errout.str()); + // Test suggested correction doesn't report an error check("void foo(std::string::iterator& i) {\n" " if (i != str.end() && std::isalpha(*i)) {\n" @@ -2360,6 +2368,14 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str()); + check("void foo(std::string::iterator& i) {\n" + " do {\n" + " std::cout << *i;\n" + " i ++;\n" + " } while (std::isalpha(*i) && i != str.end());\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (warning) Possible dereference of an invalid iterator: i\n", errout.str()); + // Test "while" with "||" case check("void foo(std::string::iterator& i) {\n" " while (!(!std::isalpha(*i) || i == str.end())) {\n"