Constant array index outside the declared bounds

Constant array index outside the declared bounds

Description

Reading or writing an element outside a fixed-size array has undefined behavior.

Potential impact

An out-of-bounds read can expose data. A write can corrupt memory or crash the program.

Remediation

Check constant indices and loop conditions against the element count. Each index must be at least zero and less than that count.

Examples

Before

c
int values[4];
values[4] = 1;

After

c
int values[4];
values[3] = 1;

For the four-element array declared as values[4], the last element is values[3].

References