| 128 | } |
| 129 | |
| 130 | void CheckStlImpl::outOfBounds() |
| 131 | { |
| 132 | logChecker("CheckStl::outOfBounds"); |
| 133 | |
| 134 | for (const Scope *function : mTokenizer->getSymbolDatabase()->functionScopes) { |
| 135 | for (const Token *tok = function->bodyStart; tok != function->bodyEnd; tok = tok->next()) { |
| 136 | const Library::Container *container = getLibraryContainer(tok); |
| 137 | if (!container || container->stdAssociativeLike) |
| 138 | continue; |
| 139 | const Token * parent = astParentSkipParens(tok); |
| 140 | const Token* accessTok = parent; |
| 141 | if (Token::simpleMatch(accessTok, ".") && Token::simpleMatch(accessTok->astParent(), "(")) |
| 142 | accessTok = accessTok->astParent(); |
| 143 | if (astIsIterator(accessTok) && Token::simpleMatch(accessTok->astParent(), "+")) |
| 144 | accessTok = accessTok->astParent(); |
| 145 | const Token* indexTok = getContainerIndex(container, parent); |
| 146 | if (indexTok == tok) |
| 147 | continue; |
| 148 | for (const ValueFlow::Value &value : tok->values()) { |
| 149 | if (!value.isContainerSizeValue()) |
| 150 | continue; |
| 151 | if (value.isImpossible()) |
| 152 | continue; |
| 153 | if (value.isInconclusive() && !mSettings.certainty.isEnabled(Certainty::inconclusive)) |
| 154 | continue; |
| 155 | if (!value.errorSeverity() && !mSettings.severity.isEnabled(Severity::warning)) |
| 156 | continue; |
| 157 | if (value.intvalue == 0 && (indexTok || |
| 158 | (containerYieldsElement(container, parent) && !containerAppendsElement(container, parent)) || |
| 159 | containerPopsElement(container, parent))) { |
| 160 | std::string indexExpr; |
| 161 | if (indexTok && !indexTok->hasKnownValue()) |
| 162 | indexExpr = indexTok->expressionString(); |
| 163 | outOfBoundsError(accessTok, tok->expressionString(), &value, indexExpr, nullptr); |
| 164 | continue; |
| 165 | } |
| 166 | if (indexTok) { |
| 167 | std::vector<ValueFlow::Value> indexValues = |
| 168 | ValueFlow::isOutOfBounds(value, indexTok, mSettings.severity.isEnabled(Severity::warning)); |
| 169 | if (!indexValues.empty()) { |
| 170 | outOfBoundsError( |
| 171 | accessTok, tok->expressionString(), &value, indexTok->expressionString(), &indexValues.front()); |
| 172 | continue; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | if (indexTok && !indexTok->hasKnownIntValue()) { |
| 177 | const ValueFlow::Value* value = |
| 178 | ValueFlow::findValue(indexTok->values(), mSettings, [&](const ValueFlow::Value& v) { |
| 179 | if (!v.isSymbolicValue()) |
| 180 | return false; |
| 181 | if (v.isImpossible()) |
| 182 | return false; |
| 183 | if (v.intvalue < 0) |
| 184 | return false; |
| 185 | const Token* sizeTok = v.tokvalue; |
| 186 | if (sizeTok && sizeTok->isCast()) |
| 187 | sizeTok = sizeTok->astOperand2() ? sizeTok->astOperand2() : sizeTok->astOperand1(); |
no test coverage detected