This error occurs when unvalidated user input is used as the index of an array or by methods that will use it as the index of an array.

Vulnerability and risk

When unvalidated user input is used as the index of an array or by methods that will use it as the index of an array, an attack can cause the methods to throw an ArrayOutOfBounds exception. This can then be used to cause Denial of Service or to manipulate the stated object to create an alternate control flow, which could be used to the advantage of the attacker. For example, if an error is printed, it might leak some information to the attacker. If an error occurred during authentication, it could create open-on-fail conditions, allowing the attacker to get an unauthorized session.

Klocwork security vulnerability (SV) checkers identify calls that create potentially dangerous data; these calls are considered unsafe sources. An unsafe source can be any data provided by the user, since the user could be an attacker or has the potential for introducing human error.

Mitigation and prevention

To prevent denial-of-service (DoS) attacks from user input, validate any and all input from outside the application (user input, file input, system parameters, and so on). Validation should include bounds validation using information from an appropriate, accessible container.

Example 1

16     protected synchronized void doPost(HttpServletRequest req, HttpServletResponse resp)
17             throws ServletException, IOException {
18         String indexString = req.getParameter("index");
19         final int index;
20         try {
21             index = Integer.parseInt(indexString);
22         } catch (NumberFormatException e) {
23             resp.sendError(505, "Internal Error");
24             return;
25         }
27         String key = data[index];
28         resp.getOutputStream().println("Success! " + key);
29     }

SV.DOS.ARRINDEX is reported for line 27: 'indexString' contains a value coming from an HTTP request and thus can be tainted (line 18). On line 21 tainted 'indexString' is parsed and stored in 'index' which is used to access an array element on line 27. A potential attacker can specify a large number, leading to an ArrayOutOfBounds exception, which can damage the state of the application and lead to a DoS attack.

Security training

Application security training materials provided by Secure Code Warrior.


This checker can be extended through the Klocwork knowledge base. See Tuning Java analysis for more information.