Support "else if" and do-loop in CheckStl::checkDereferenceInvalidIterator()

This commit is contained in:
PKEuS 2014-04-12 19:54:31 +02:00
parent e8ac355b39
commit 8cb3b13e56
2 changed files with 21 additions and 1 deletions

View File

@ -1471,10 +1471,14 @@ void CheckStl::checkDereferenceInvalidIterator()
// be an iterator that is dereferenced before being checked for validity.
const std::list<Scope>& scopeList = _tokenizer->getSymbolDatabase()->scopeList;
for (std::list<Scope>::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;

View File

@ -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"