diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 418933836..f840768a7 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -308,12 +308,14 @@ void CheckStl::stlOutOfBounds() for (std::list::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { const Token* tok = i->classDef; // only interested in conditions - if ((i->type != Scope::eFor && i->type != Scope::eWhile && i->type != Scope::eIf) || !tok) + if ((i->type != Scope::eFor && i->type != Scope::eWhile && i->type != Scope::eIf && i->type != Scope::eDo) || !tok) continue; if (i->type == Scope::eFor) tok = Token::findsimplematch(tok->tokAt(2), ";"); - else + else if (i->type == Scope::eDo) { + tok = tok->linkAt(1)->tokAt(2); + } else tok = tok->next(); // check if the for loop condition is wrong @@ -331,7 +333,7 @@ void CheckStl::stlOutOfBounds() // variable id for the container variable const unsigned int declarationId = container->declarationId(); - for (const Token *tok3 = tok->tokAt(8); tok3 && tok3 != i->classEnd; tok3 = tok3->next()) { + for (const Token *tok3 = i->classStart; tok3 && tok3 != i->classEnd; tok3 = tok3->next()) { if (tok3->varId() == declarationId) { if (Token::Match(tok3->next(), ". size|length ( )")) break; diff --git a/test/teststl.cpp b/test/teststl.cpp index 389b174a4..c3d5c30c7 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -547,6 +547,14 @@ private: "}"); ASSERT_EQUALS("[test.cpp:3]: (error) When ii==foo.size(), foo[ii] is out of bounds.\n", errout.str()); + check("void foo(const std::string& foo, unsigned int ii) {\n" + " do {\n" + " foo[ii] = 'x';\n" + " ++i;\n" + " } while(ii <= foo.length());\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) When ii==foo.size(), foo[ii] is out of bounds.\n", errout.str()); + check("void foo(const std::string& foo, unsigned int ii) {\n" " if (anything()) {\n" " } else if (ii <= foo.length()) {\n"