Fixed #3865 (Suspicious condition. The result of find is an iterator, but it is not properly checked.)

This commit is contained in:
Daniel Marjamäki 2012-06-07 19:33:18 +02:00
parent 9026febaad
commit 2b3e5abef8
2 changed files with 25 additions and 3 deletions

View File

@ -721,6 +721,22 @@ void CheckStl::stlBoundriesError(const Token *tok, const std::string &container_
"iterators in the container.");
}
static bool if_findCompare(const Token * const tokBack)
{
const Token *tok = tokBack;
while (tok && tok->str() == ")") {
tok = tok->next();
if (Token::Match(tok,",|==|!="))
return true;
if (Token::Match(tok, ") !!{") &&
tok->link()->previous() &&
(Token::Match(tok->link()->previous(),",|==|!=") ||
tok->link()->previous()->isName()))
return true;
}
return false;
}
void CheckStl::if_find()
{
if (!_settings->isEnabled("style"))
@ -749,7 +765,7 @@ void CheckStl::if_find()
tok = tok->next();
if (Token::Match(tok, "%var% . find (")) {
if (!Token::Match(tok->linkAt(3), ") &&|)|%oror%"))
if (if_findCompare(tok->linkAt(3)))
continue;
const unsigned int varid = tok->varId();
@ -780,7 +796,7 @@ void CheckStl::if_find()
if (!Token::simpleMatch(tok2, ". find ("))
continue;
if (!Token::Match(tok2->linkAt(2), ") &&|)|%oror%"))
if (if_findCompare(tok2->linkAt(2)))
continue;
const unsigned int varid = tok->varId();
@ -827,7 +843,7 @@ void CheckStl::if_find()
else if (Token::Match(tok, "std :: find|find_if (")) {
// check that result is checked properly
if (Token::Match(tok->linkAt(3), ") &&|)|%oror%")) {
if (!if_findCompare(tok->linkAt(3))) {
if_findError(tok, false);
}
}

View File

@ -1268,6 +1268,12 @@ private:
check("void f() {\n"
" if (()) { }\n"
"}");
// #3865
check("void f() {\n"
" if ((std::find(a,b,c)) != b) { }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void if_str_find() {