Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

User-controlled data in numeric cast

ID: java/tainted-numeric-castKind: path-problemSecurity severity: 9.0Severity: errorPrecision: highTags:   - security   - external/cwe/cwe-197   - external/cwe/cwe-681Query suites:   - java-code-scanning.qls   - java-security-extended.qls   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

Casting a user-controlled numeric value to a narrower type can result in truncated values unless the input is validated.

Narrowing conversions may cause potentially unintended results. For example, casting the positive integer value128 to typebyte yields the negative value-128.

Recommendation

Guard against unexpected truncation of user-controlled arithmetic data by doing one of the following:

  • Validate the user input.

  • Define a guard on the cast expression, so that the cast is performed only if the input is known to be within the range of the resulting type.

  • Avoid casting to a narrower type, and instead continue to use a wider type.

Example

In this example, a value is read from standard input into along. Because the value is a user-controlled value, it could be extremely large. Casting this value to a narrower type could therefore cause unexpected truncation. Thescaled2 example uses a guard to avoid this problem and checks the range of the input before performing the cast. If the value is too large to cast to typeint it is rejected as invalid.

classTest{publicstaticvoidmain(String[]args)throwsIOException{{longdata;BufferedReaderreaderBuffered=newBufferedReader(newInputStreamReader(System.in,"UTF-8"));StringstringNumber=readerBuffered.readLine();if(stringNumber!=null){data=Long.parseLong(stringNumber.trim());}else{data=0;}// AVOID: potential truncation if input data is very large,// for example 'Long.MAX_VALUE'intscaled=(int)data;//...// GOOD: use a guard to ensure no truncation occursintscaled2;if(data>Integer.MIN_VALUE&&data<Integer.MAX_VALUE)scaled2=(int)data;elsethrownewIllegalArgumentException("Invalid input");}}}

References


[8]ページ先頭

©2009-2025 Movatter.jp