AutosarC++17_10-A5.2.5

An array or container shall not be accessed beyond its range

Required inputs: IR, StaticSemanticAnalysis

Rationale

To avoid undefined behavior, range checks should be coded to ensure that the array access via pointer arithmetic or subscript operator is within defined bounds.

This could be also achieved by accessing an array via subscript operator with constant indexes only.

Note that this rule applies to C-style arrays and all other containers that access their elements using input index without range-checks.

Also, note that calculating an address one past the last element of the array is well defined, but dereferencing it is not.

Example
// $Id: A5-2-5.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <array>
#include <cstdint>
#include <iostream>
void Fn1() noexcept {
  constexpr std::int32_t arraySize = 16;
  std::int32_t array[arraySize]{0};

  std::int32_t elem1 = array[0]; // Compliant - access with constant literal
                                 // that is less than ArraySize
  std::int32_t elem2 = array[12]; // Compliant - access with constant literal
                                  // that is less than ArraySize
  for (std::int32_t idx = 0; idx < 20; ++idx) {
    std::int32_t elem3 = array[idx]; // Non-compliant - access beyond ArraySize
                                     // bounds, which has 16 elements
  }

  std::int32_t shift = 25;
  std::int32_t elem4 =
      *(array + shift); // Non-compliant - access beyond ArraySize bounds

  std::int32_t index = 0;
  std::cin >> index;
  std::int32_t elem5 =
      array[index]; // Non-compliant - index may exceed the ArraySize bounds
  if (index < arraySize) {
    std::int32_t elem6 = array[index]; // Compliant - range check coded
  }
}
void Fn2() noexcept {
  constexpr std::int32_t arraySize = 32;
  std::array<std::int32_t, arraySize> array;
  array.fill(0);

  std::int32_t elem1 = array[10]; // Compliant - access with constant literal
                                  // that is less than ArraySize
  std::int32_t index = 40;
  std::int32_t elem2 =
      array[index]; // Non-compliant - access beyond ArraySize bounds
  try {
    std::int32_t elem3 = array.at(50); // Compliant - at() method provides a
                                       // range check, throwing an exception if
                                       // input exceeds the bounds
  } catch (std::out_of_range &) {
    // Handle an error
  }

  for (auto &&e : array) // The std::array provides a possibility to iterate
                         // over its elements with range-based loop
  {
    // Iterate over all elements
  }
}
See Also
  • HIC++ v4.0 [8]: 5.2.1 Ensure that pointer or array access is demonstrably within bounds of a valid object.

Possible Messages

Key

Text

Severity

Disabled

arithmetic_out_of_bounds

Pointer arithmetic on {node0} might create pointer outside array bounds of {name0}

None

False

out_of_bounds

Access into array is out of bounds

None

False

possible_indirect_out_of_bounds

Pointer-indirect access through {node0} might be out of bounds accessing {name0}

None

False

possible_out_of_bounds

Access into array might be out of bounds

None

False

undereferenced_arithmetic_out_of_bounds

Pointer arithmetic on {node0} might create pointer one past the end of {name0} (but not dereferenced)

None

False

undereferenced_out_of_bounds

Access is one past the end of the array (but not dereferenced)

None

False

undereferenced_possible_indirect_out_of_bounds

Pointer-indirect access through {node0} might be one past the end accessing {name0} (but not dereferenced)

None

False

undereferenced_possible_out_of_bounds

Access might be one past the end of the array (but not dereferenced)

None

False

Options

abstract_interpretation_out_of_bounds

abstract_interpretation_out_of_bounds : bool = False

Use additional "symbolic expression analysis" as postprocessing step. This can remove false positives, but might require more time. Option is automatically active if option StaticSemanticAnalysis/performance.general.enhanced_analysis is active.
 

exclude_very_high_indices

exclude_very_high_indices : bool = True

Enables heuristic to detect false positives: When index used for array access is very high in comparison to the array's size, assume false positive.
 

report_unbounded_arrays

report_unbounded_arrays : bool = False

If true, accesses into arrays with unknown bound are reported as being potentially outside the allowed range. This affects arrays like extern char buf[];.
 

report_undereferenced_one_past_the_end

report_undereferenced_one_past_the_end : bool = False

If true, report accesses one past the end of an array even if there is no dereference of the resulting pointer.
 

report_unknown_index

report_unknown_index : bool = False

If false, do not report possible out-of-bound findings for which the analysis was not able to infer any restricting information about the array index (this can lead to excluding both false positives and true findings).