Performing calculations on uncontrolled data can result in integer overflows unless the input is validated.

If the data is not under your control, and can take extremely large values, even arithmetic operations that would usually result in a small change in magnitude may result in overflows.

Always guard against overflow in arithmetic operations on uncontrolled data by doing one of the following:

In this example, a random integer is generated. Because the value is not controlled by the programmer, it could be extremely large. Performing arithmetic operations on this value could therefore cause an overflow. To avoid this happening, the example shows how to perform a check before performing a multiplication.

  • SEI CERT Oracle Coding Standard for Java: NUM00-J. Detect or prevent integer overflow.