Support do-loops in CheckStl::stlOutOfBounds()

This commit is contained in:
PKEuS 2014-10-02 15:19:01 +02:00
parent 621644b17a
commit bb8c8d53cc
2 changed files with 13 additions and 3 deletions

View File

@ -308,12 +308,14 @@ void CheckStl::stlOutOfBounds()
for (std::list<Scope>::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;

View File

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