-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathBadAbsOfRandom.ql
More file actions
24 lines (23 loc) · 904 Bytes
/
BadAbsOfRandom.ql
File metadata and controls
24 lines (23 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @name Incorrect absolute value of random number
* @description Calling 'Math.abs' to find the absolute value of a randomly generated integer is not
* guaranteed to return a non-negative integer.
* @kind problem
* @problem.severity warning
* @precision medium
* @id java/abs-of-random
* @tags reliability
* maintainability
*/
import java
from MethodAccess ma, Method abs, Method nextIntOrLong, MethodAccess nma
where
ma.getMethod() = abs and
abs.hasName("abs") and
abs.getDeclaringType().hasQualifiedName("java.lang","Math") and
ma.getAnArgument() = nma and
nma.getMethod() = nextIntOrLong and
(nextIntOrLong.hasName("nextInt") or nextIntOrLong.hasName("nextLong")) and
nextIntOrLong.getDeclaringType().hasQualifiedName("java.util","Random") and
nextIntOrLong.hasNoParameters()
select ma, "Incorrect computation of abs of signed integral random value."