Skip to content

Commit 40a7f5a

Browse files
committed
Java: Minor fix to modulus analysis to handle constant expressions and not only compile time constants
1 parent 5688210 commit 40a7f5a

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

java/ql/src/semmle/code/java/dataflow/ModulusAnalysis.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ private Expr modExpr(Expr arg, int mod) {
6868
exists(RemExpr rem |
6969
result = rem and
7070
arg = rem.getLeftOperand() and
71-
rem.getRightOperand().(CompileTimeConstantExpr).getIntValue() = mod and
71+
rem.getRightOperand().(ConstantIntegerExpr).getIntValue() = mod and
7272
mod >= 2
7373
)
7474
or
75-
exists(CompileTimeConstantExpr c |
75+
exists(ConstantIntegerExpr c |
7676
mod = 2.pow([1 .. 30]) and
7777
c.getIntValue() = mod - 1 and
7878
result.(AndBitwiseExpr).hasOperands(arg, c)
@@ -84,7 +84,7 @@ private Expr modExpr(Expr arg, int mod) {
8484
* its `testIsTrue` branch.
8585
*/
8686
private Guard moduloCheck(SsaVariable v, int val, int mod, boolean testIsTrue) {
87-
exists(Expr rem, CompileTimeConstantExpr c, int r, boolean polarity |
87+
exists(Expr rem, ConstantIntegerExpr c, int r, boolean polarity |
8888
result.isEquality(rem, c, polarity) and
8989
c.getIntValue() = r and
9090
rem = modExpr(v.getAUse(), mod) and

java/ql/test/library-tests/dataflow/modulus-analysis/ModulusAnalysis.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| ModulusAnalysis.java:12:25:12:29 | seven | 0 | 7 | 0 |
2323
| ModulusAnalysis.java:12:25:12:29 | seven | SSA def(seven) | 0 | 0 |
2424
| ModulusAnalysis.java:13:32:13:34 | mul | 0 | 3 | 42 |
25+
| ModulusAnalysis.java:13:32:13:34 | mul | 0 | 7 | 43 |
2526
| ModulusAnalysis.java:13:32:13:34 | mul | SSA def(mul) | 0 | 0 |
2627
| ModulusAnalysis.java:16:17:18:23 | ...?...:... | 0 | 3 | 4 |
2728
| ModulusAnalysis.java:17:15:17:15 | i | SSA init(i) | 0 | 0 |

java/ql/test/library-tests/dataflow/modulus-analysis/ModulusAnalysis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void m(int i, boolean cond, int x, int y, int[] arr) {
1010

1111
int seven = 7;
1212
if (mul % c2 == seven) {
13-
System.out.println(mul); // congruent 3 mod 42
13+
System.out.println(mul); // congruent 3 mod 42, 7 mod 43
1414
}
1515

1616
int j = cond

0 commit comments

Comments
 (0)