Array index outside bounds during array access
This defect occurs
when an array index falls outside the range [0...array_size-1] during
array access.
Accessing an array outside its bounds is undefined behavior. You can read an unpredictable value or try to access a location that is not allowed and encounter a segmentation fault.
The fix depends on the root cause of the defect. For instance, you accessed an array inside a loop and one of these situations happened:
The upper bound of the loop is too large.
You used an array index that is the same as the loop index instead of being one less than the loop index.
To fix the issue, you have to modify the loop bound or the array index.
Another reason why an array index can exceed array bounds is a prior conversion from signed to unsigned integers. The conversion can result in a wrap around of the index value, eventually causing the array index to exceed the array bounds.
Often the result details show a sequence of events that led to the defect. You can implement the fix on any event in the sequence. If the result details do not show the event history, you can trace back using right-click options in the source code and see previous related events. See also Interpret Bug Finder Results in Polyspace Desktop User Interface.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See Address Polyspace Results Through Bug Fixes or Justifications.
| Group: Static memory |
| Language: C | C++ |
| Default: On |
Command-Line Syntax: OUT_BOUND_ARRAY |
| Impact: High |
| CWE ID: 119, 131, 466 |