Why is this an issue?

An array index out-of-bounds panic is a bug class that occurs in Rust when a program tries to access an array element that does not exist. This bug can cause your program to crash or behave unexpectedly.

What is the potential impact?

Issues of this type interrupt the normal execution of a program, causing it to crash or putting it into an inconsistent state. Therefore, this issue might impact the availability and reliability of your application, or even result in data loss.

If the computation of an index value is tied to user input data, this issue can potentially even be exploited by attackers to disrupt your application.

How to fix it

To fix an array index out of bounds panic in Rust, you should always ensure that you are accessing array elements within the bounds of the array.

Code examples

Noncompliant code example

let x = [1, 2, 3, 4];

x[9]; // Out of bounds indexing

Compliant solution

let x = [1, 2, 3, 4];

x[0];

Resources

Documentation