Skip to content

Commit 92d6135

Browse files
committed
Remove abstract class RandomNumberGenerator
1 parent 410f21c commit 92d6135

File tree

2 files changed

+7
-34
lines changed

2 files changed

+7
-34
lines changed

java/ql/src/Likely Bugs/Arithmetic/RandomUsedOnce.ql

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
import java
1515
import semmle.code.java.security.Random
1616

17-
from MethodAccess ma, Method random
18-
where
19-
random.getDeclaringType() instanceof RandomNumberGenerator and
20-
ma.getMethod() = random and
21-
ma.getQualifier() instanceof ClassInstanceExpr
17+
from RandomDataSource ma
18+
where ma.getQualifier() instanceof ClassInstanceExpr
2219
select ma, "Random object created and used only once."

java/ql/src/semmle/code/java/security/Random.qll

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,17 @@ import java
22
import semmle.code.java.dataflow.DefUse
33
import semmle.code.java.dataflow.DataFlow
44

5-
/**
6-
* A class with methods that generate random data.
7-
*/
8-
abstract class RandomNumberGenerator extends RefType { }
9-
105
/**
116
* The `java.security.SecureRandom` class.
127
*/
13-
class SecureRandomNumberGenerator extends RandomNumberGenerator {
8+
class SecureRandomNumberGenerator extends RefType {
149
SecureRandomNumberGenerator() { this.hasQualifiedName("java.security", "SecureRandom") }
1510
}
1611

17-
/**
18-
* The `java.util.Random` class or any of its subtypes, including `java.security.SecureRandom`.
19-
*/
20-
class StdlibRandom extends RandomNumberGenerator {
21-
StdlibRandom() { this.getAnAncestor().hasQualifiedName("java.util", "Random") }
22-
}
23-
24-
/**
25-
* The `org.apache.commons.lang3.RandomUtils` class.
26-
*/
27-
class ApacheRandomUtils extends RandomNumberGenerator {
28-
ApacheRandomUtils() { this.hasQualifiedName("org.apache.commons.lang3", "RandomUtils") }
29-
}
30-
3112
/**
3213
* A method access that returns random data or writes random data to an argument.
3314
*/
3415
abstract class RandomDataSource extends MethodAccess {
35-
RandomDataSource() {
36-
exists(Method m | m = this.getMethod() |
37-
m.getName().matches("next%") and
38-
m.getDeclaringType() instanceof RandomNumberGenerator
39-
)
40-
}
41-
4216
/**
4317
* Gets the integer lower bound, inclusive, of the values returned by this call,
4418
* if applicable to this method's type and a constant bound is known.
@@ -85,7 +59,8 @@ class StdlibRandomSource extends RandomDataSource {
8559

8660
StdlibRandomSource() {
8761
m = this.getMethod() and
88-
m.getDeclaringType() instanceof StdlibRandom
62+
m.getName().matches("next%") and
63+
m.getDeclaringType().getAnAncestor().hasQualifiedName("java.util", "Random")
8964
}
9065

9166
// Note for the following bounds functions: `java.util.Random` only defines no-arg versions
@@ -146,7 +121,8 @@ class ApacheCommonsRandomSource extends RandomDataSource {
146121

147122
ApacheCommonsRandomSource() {
148123
m = this.getMethod() and
149-
m.getDeclaringType() instanceof ApacheRandomUtils
124+
m.getName().matches("next%") and
125+
m.getDeclaringType().hasQualifiedName("org.apache.commons.lang3", "RandomUtils")
150126
}
151127

152128
override Expr getLowerBoundExpr() {

0 commit comments

Comments
 (0)