stl: removed false positives for STL buffer overruns. Bailing out when it can't be checked if the index is ok or not. (#285)

This commit is contained in:
Daniel Marjamäki 2009-05-06 21:55:04 +02:00
parent a32114b15c
commit 80fe293c19
2 changed files with 29 additions and 43 deletions

View File

@ -86,59 +86,45 @@ void CheckStl::stlOutOfBounds()
if (!Token::simpleMatch(tok, "for ("))
continue;
int indent = 1;
tok = tok->tokAt(2);
const Token *num = 0;
const Token *var = 0;
while (tok)
unsigned int indent = 0;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok->str() == "(")
if (tok2->str() == "(")
++indent;
if (tok->str() == ")")
else if (tok2->str() == ")")
{
--indent;
if (indent == 0)
break;
}
if (Token::Match(tok, "; %var% <= %var% . size ( ) ;"))
{
num = tok->tokAt(1);
var = tok->tokAt(3);
}
tok = tok->next();
}
if (!tok)
return;
tok = tok->next();
if (!num || tok->str() != "{")
continue;
std::string pattern = var->str() + " [ " + num->str() + " ]";
while (tok)
{
if (tok->str() == "{")
++indent;
if (tok->str() == "}")
{
--indent;
if (indent == 0)
break;
}
if (Token::Match(tok, pattern.c_str()))
if (Token::Match(tok2, "; %var% <= %var% . size ( ) ;"))
{
stlOutOfBoundsError(tok, num->str(), var->str());
indent = 0;
const std::string num(tok2->strAt(1));
const std::string varname(tok2->strAt(3));
for (const Token *tok3 = tok2->tokAt(8); tok3; tok3 = tok3->next())
{
if (tok3->str() == "{")
++indent;
else if (tok3->str() == "}")
{
if (indent == 0)
break;
--indent;
}
else if (tok3->str() == varname)
{
if (Token::simpleMatch(tok3->next(), ". size ( )"))
break;
else if (Token::simpleMatch(tok3->next(), ("[ " + num + " ]").c_str()))
stlOutOfBoundsError(tok3, num, varname);
}
}
break;
}
tok = tok->next();
}
}
}

View File

@ -188,7 +188,7 @@ private:
" }\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS(std::string(""), errout.str());
ASSERT_EQUALS(std::string(""), errout.str());
}
}