GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Uncontrolled data in arithmetic expression — CodeQL query help documentation

Uncontrolled data in arithmetic expression¶

ID: java/uncontrolled-arithmetic
Kind: path-problem
Security severity: 8.6
Severity: warning
Precision: medium
Tags:
   - security
   - external/cwe/cwe-190
   - external/cwe/cwe-191
Query suites:
   - java-security-extended.qls
   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

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.

Recommendation¶

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

  • Validate the data.

  • Define a guard on the arithmetic expression, so that the operation is performed only if the result can be known to be less than, or equal to, the maximum value for the type, for example MAX_VALUE.

  • Use a wider type, so that larger input values do not cause overflow.

Example¶

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.

class Test {
	public static void main(String[] args) {
		{
			int data = (new java.security.SecureRandom()).nextInt();

			// BAD: may overflow if data is large
			int scaled = data * 10;

			// ...

			// GOOD: use a guard to ensure no overflows occur
			int scaled2;
			if (data < Integer.MAX_VALUE/10)
				scaled2 = data * 10;
			else 
				scaled2 = Integer.MAX_VALUE;
		}
	}
}

References¶

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

  • Common Weakness Enumeration: CWE-190.

  • Common Weakness Enumeration: CWE-191.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.