@@ -1409,56 +1409,56 @@ void CheckStl::checkDereferenceInvalidIterator()
14091409 // be an iterator that is dereferenced before being checked for validity.
14101410 const std::list<Scope>& scopeList = _tokenizer->getSymbolDatabase ()->scopeList ;
14111411 for (std::list<Scope>::const_iterator i = scopeList.begin (); i != scopeList.end (); ++i) {
1412- if (i->type == Scope::eIf || i->type == Scope::eDo || i->type == Scope::eWhile || i->type == Scope::eFor) {
1412+ if (!(i->type == Scope::eIf || i->type == Scope::eDo || i->type == Scope::eWhile || i->type == Scope::eFor))
1413+ continue ;
1414+
1415+ const Token* const tok = i->classDef ;
1416+ const Token* startOfCondition = tok->next ();
1417+ if (i->type == Scope::eDo)
1418+ startOfCondition = startOfCondition->link ()->tokAt (2 );
1419+ if (!startOfCondition) // ticket #6626 invalid code
1420+ continue ;
1421+ const Token* endOfCondition = startOfCondition->link ();
1422+ if (!endOfCondition)
1423+ continue ;
14131424
1414- const Token* const tok = i->classDef ;
1415- const Token* startOfCondition = tok->next ();
1416- if (i->type == Scope::eDo)
1417- startOfCondition = startOfCondition->link ()->tokAt (2 );
1418- if (!startOfCondition) // ticket #6626 invalid code
1425+ // For "for" loops, only search between the two semicolons
1426+ if (i->type == Scope::eFor) {
1427+ startOfCondition = Token::findsimplematch (tok->tokAt (2 ), " ;" , endOfCondition);
1428+ if (!startOfCondition)
14191429 continue ;
1420- const Token* endOfCondition = startOfCondition->link ( );
1430+ endOfCondition = Token::findsimplematch ( startOfCondition->next (), " ; " , endOfCondition );
14211431 if (!endOfCondition)
14221432 continue ;
1433+ }
14231434
1424- // For "for" loops, only search between the two semicolons
1425- if (i->type == Scope::eFor) {
1426- startOfCondition = Token::findsimplematch (tok->tokAt (2 ), " ;" , endOfCondition);
1427- if (!startOfCondition)
1428- continue ;
1429- endOfCondition = Token::findsimplematch (startOfCondition->next (), " ;" , endOfCondition);
1430- if (!endOfCondition)
1431- continue ;
1432- }
1433-
1434- // Only consider conditions composed of all "&&" terms and
1435- // conditions composed of all "||" terms
1436- const bool isOrExpression =
1437- Token::findsimplematch (startOfCondition, " ||" , endOfCondition) != 0 ;
1438- const bool isAndExpression =
1439- Token::findsimplematch (startOfCondition, " &&" , endOfCondition) != 0 ;
1440-
1441- // Look for a check of the validity of an iterator
1442- const Token* validityCheckTok = 0 ;
1443- if (!isOrExpression && isAndExpression) {
1444- validityCheckTok =
1445- Token::findmatch (startOfCondition, " && %var% != %name% . end|rend|cend|crend ( )" , endOfCondition);
1446- } else if (isOrExpression && !isAndExpression) {
1447- validityCheckTok =
1448- Token::findmatch (startOfCondition, " %oror% %var% == %name% . end|rend|cend|crend ( )" , endOfCondition);
1449- }
1450-
1451- if (!validityCheckTok)
1452- continue ;
1453- const unsigned int iteratorVarId = validityCheckTok->next ()->varId ();
1454-
1455- // If the iterator dereference is to the left of the check for
1456- // the iterator's validity, report an error.
1457- const Token* const dereferenceTok =
1458- Token::findmatch (startOfCondition, " * %varid%" , validityCheckTok, iteratorVarId);
1459- if (dereferenceTok)
1460- dereferenceInvalidIteratorError (dereferenceTok, dereferenceTok->strAt (1 ));
1435+ // Only consider conditions composed of all "&&" terms and
1436+ // conditions composed of all "||" terms
1437+ const bool isOrExpression =
1438+ Token::findsimplematch (startOfCondition, " ||" , endOfCondition) != 0 ;
1439+ const bool isAndExpression =
1440+ Token::findsimplematch (startOfCondition, " &&" , endOfCondition) != 0 ;
1441+
1442+ // Look for a check of the validity of an iterator
1443+ const Token* validityCheckTok = 0 ;
1444+ if (!isOrExpression && isAndExpression) {
1445+ validityCheckTok =
1446+ Token::findmatch (startOfCondition, " && %var% != %name% . end|rend|cend|crend ( )" , endOfCondition);
1447+ } else if (isOrExpression && !isAndExpression) {
1448+ validityCheckTok =
1449+ Token::findmatch (startOfCondition, " %oror% %var% == %name% . end|rend|cend|crend ( )" , endOfCondition);
14611450 }
1451+
1452+ if (!validityCheckTok)
1453+ continue ;
1454+ const unsigned int iteratorVarId = validityCheckTok->next ()->varId ();
1455+
1456+ // If the iterator dereference is to the left of the check for
1457+ // the iterator's validity, report an error.
1458+ const Token* const dereferenceTok =
1459+ Token::findmatch (startOfCondition, " * %varid%" , validityCheckTok, iteratorVarId);
1460+ if (dereferenceTok)
1461+ dereferenceInvalidIteratorError (dereferenceTok, dereferenceTok->strAt (1 ));
14621462 }
14631463}
14641464
0 commit comments