Skip to main content

IAR Embedded Workbench for RX 5.20

ARR-inv-index-pos

In this section:
Synopsis

An array access might be out of bounds, depending on which path is executed.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

An element of an array is accessed, but one or more of the executable paths means that the element is outside the bounds of the array. This might corrupt data and/or crash the application, and result in security vulnerabilities. This check is identical to MISRAC++2008-5-0-16_d, MISRAC2012-Rule-18.1_b, CERT-ARR30-C_b.

Coding standards
CERT ARR30-C

Do not form or use out of bounds pointers or array subscripts

CWE 119

Improper Restriction of Operations within the Bounds of a Memory Buffer

CWE 120

Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')

CWE 121

Stack-based Buffer Overflow

CWE 124

Buffer Underwrite ('Buffer Underflow')

CWE 126

Buffer Over-read

CWE 127

Buffer Under-read

CWE 129

Improper Validation of Array Index

MISRA C:2012 Rule-18.1

(Required) A pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operand

MISRA C++ 2008 5-0-16

(Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.

Code examples

The following code example fails the check and will give a warning:

int cond;

int main(void)
{
  int a[7];
  int x;

  if (cond)
    x = 3;
  else
    x = 20;

  a[x] = 0;  //x may be set to 20 in line 11
             //but a only has an interval of [0,6]
  return 0;
}

The following code example passes the check and will not give a warning about this issue:

int cond;

int main(void)
{
  int a[25];
  int x;

  if (cond)
    x = 3;
  else
    x = 20;

  a[x] = 0;  //here, both possible values of
             //x are in the interval [0,24]
  return 0;
}