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

Method arrayIndexError

lib/checkbufferoverrun.cpp:408–435  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

406}
407
408void CheckBufferOverrunImpl::arrayIndexError(const Token* tok,
409 const std::vector<Dimension>& dimensions,
410 const std::vector<ValueFlow::Value>& indexes)
411{
412 if (!tok) {
413 reportError(tok, Severity::error, "arrayIndexOutOfBounds", "Array 'arr[16]' accessed at index 16, which is out of bounds.", CWE_BUFFER_OVERRUN, Certainty::normal);
414 reportError(tok, Severity::warning, "arrayIndexOutOfBoundsCond", "Array 'arr[16]' accessed at index 16, which is out of bounds.", CWE_BUFFER_OVERRUN, Certainty::normal);
415 return;
416 }
417
418 const Token *condition = nullptr;
419 const ValueFlow::Value *index = nullptr;
420 for (const ValueFlow::Value& indexValue : indexes) {
421 if (!indexValue.errorSeverity() && !mSettings.severity.isEnabled(Severity::warning))
422 return;
423 if (indexValue.condition)
424 condition = indexValue.condition;
425 if (!index || !indexValue.errorPath.empty())
426 index = &indexValue;
427 }
428
429 reportError(getErrorPath(tok, index, "Array index out of bounds"),
430 index->errorSeverity() ? Severity::error : Severity::warning,
431 index->condition ? "arrayIndexOutOfBoundsCond" : "arrayIndexOutOfBounds",
432 arrayIndexMessage(tok, dimensions, indexes, condition),
433 CWE_BUFFER_OVERRUN,
434 index->isInconclusive() ? Certainty::inconclusive : Certainty::normal);
435}
436
437void CheckBufferOverrunImpl::negativeIndexError(const Token* tok,
438 const std::vector<Dimension>& dimensions,

Callers 1

getErrorMessagesMethod · 0.80

Calls 5

arrayIndexMessageFunction · 0.85
reportErrorFunction · 0.70
isEnabledMethod · 0.45
emptyMethod · 0.45
isInconclusiveMethod · 0.45

Tested by

no test coverage detected