Java code weakness and repair - INT: Suspicious integer expression

Weakness description

"INT: Suspicious integer expression", suspicious integer expression. This type of vulnerability refers to code that uses inappropriate integer expressions.

This type of vulnerability warning appears in many scenarios, such as:

  1. In a conditional statement, an inappropriate integer comparison operator is used, for example, in code such as if (i == true), a boolean comparison operator should be used instead of an integer comparison operator.
  2. In a loop statement, an inappropriate integer counter is used, such as in code such as for (int i = 0; i < 10; i–) where the decrement of the loop counter i may cause an infinite loop or other unexpected results .
  3. In an arithmetic expression, an inappropriate integer type or an overflow operation is used, for example, in a code such as int result = a * b, if the values ​​of a and b are large, it may cause an integer overflow, resulting in a wrong result .

sample code

public class SuspiciousIntExpress {

	public void suspiciousIngegerExpression(List<String> list){
		if(list == null ||list.size()<0) {
			System.out.println("Hello.");
		}
		
	}
}

In the example here, the minimum number of elements in List is 0, and it is impossible to be smaller than 0. In Eclipse Spotbugh, it will prompt Bad comparison of nonnegative value with 0 (wrong comparison of nonnegative value with 0). The error message is as follows:

Guess you like

Origin blog.csdn.net/oscar999/article/details/131878053