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

Function arrayIndexMessage

lib/checkbufferoverrun.cpp:388–406  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

386}
387
388static std::string arrayIndexMessage(const Token* tok,
389 const std::vector<Dimension>& dimensions,
390 const std::vector<ValueFlow::Value>& indexValues,
391 const Token* condition)
392{
393 auto add_dim = [](const std::string &s, const Dimension &dim) {
394 return s + "[" + MathLib::toString(dim.num) + "]";
395 };
396 const std::string array = std::accumulate(dimensions.cbegin(), dimensions.cend(), tok->astOperand1()->expressionString(), std::move(add_dim));
397
398 std::ostringstream errmsg;
399 if (condition)
400 errmsg << ValueFlow::eitherTheConditionIsRedundant(condition)
401 << " or the array '" << array << "' is accessed at index " << stringifyIndexes(tok->astOperand1()->expressionString(), indexValues) << ", which is out of bounds.";
402 else
403 errmsg << "Array '" << array << "' accessed at index " << stringifyIndexes(tok->astOperand1()->expressionString(), indexValues) << ", which is out of bounds.";
404
405 return errmsg.str();
406}
407
408void CheckBufferOverrunImpl::arrayIndexError(const Token* tok,
409 const std::vector<Dimension>& dimensions,

Callers 2

arrayIndexErrorMethod · 0.85
negativeIndexErrorMethod · 0.85

Calls 5

stringifyIndexesFunction · 0.85
astOperand1Method · 0.80
toStringFunction · 0.70
expressionStringMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected