MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / outOfBounds

Method outOfBounds

lib/checkstl.cpp:130–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128}
129
130void 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();

Callers 1

runChecksMethod · 0.45

Calls 15

getLibraryContainerFunction · 0.85
astParentSkipParensFunction · 0.85
astIsIteratorFunction · 0.85
getContainerIndexFunction · 0.85
containerYieldsElementFunction · 0.85
containerAppendsElementFunction · 0.85
containerPopsElementFunction · 0.85
getContainerFromSizeFunction · 0.85
nextMethod · 0.80
astParentMethod · 0.80
isImpossibleMethod · 0.80
hasKnownValueMethod · 0.80

Tested by

no test coverage detected