Accessing an array with an invalid index - either before the beginning or after the end - will result in an error at runtime.


== Noncompliant Code Example

----
int [] iArray = new int [] {0,1,2,3};

int i = iArray[4]; // Noncompliant; last valid index is 3
int j = iArray[iArray.length];  // Noncompliant

int [] iArrayCopy = new int [iArray.length];
System.arraycopy(iArray, 0, iArrayCopy, 1, iArray.length);  // Noncompliant; last element of iArray will be copied beyond end of iArrayCopy

char [] cArray = new char[] {'h', 'e', 'l', 'l', 'o'};
String str = new String(cArray, 1, cArray.length)  // Noncompliant; constructor will try to copy cArray[6], it ends at cArray[5]

for (int i = 0; i <= iArray.length; i++) {  // Noncompliant; loop will go beyond the end of the array
  LOGGER.info(iArray[i] + " is next in the array");
}
----


== See

* https://cwe.mitre.org/data/definitions/129.html[MITRE, CWE-129] - Improper Validation of Array Index

