MISRAC++2008-5-0-16_c
Synopsis
(Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.
Enabled by default
Yes
Severity/Certainty
High/High

Full description
An array access is out of bounds. This check is identical to ARR-inv-index, MISRAC2012-Rule-18.1_a, CERT-ARR30-C_a.
Coding standards
- CERT ARR33-C
Guarantee that copies are made into storage of sufficient size
- 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
Code examples
The following code example fails the check and will give a warning:
int example(int x, int y)
{
int a[10];
if((x >= 0) && (x < 20)) {
if(x < 10) {
y = a[x];
} else {
y = a[x - 10];
y = a[x];
}
}
return y;
}
The following code example passes the check and will not give a warning about this issue:
int main(void)
{
int a[4];
a[3] = 0;
return 0;
}