Skip to content

Commit 2c3a6d7

Browse files
committed
Java: Allow explicit zero multiplication in java/evaluation-to-constant.
1 parent 8a8b795 commit 2c3a6d7

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ where
7474
not child instanceof Annotation
7575
) and
7676
not e instanceof CompileTimeConstantExpr and
77+
// Exclude explicit zero multiplication.
78+
not e.(MulExpr).getAnOperand().(IntegerLiteral).getIntValue() = 0 and
7779
// Exclude expressions that appear to be disabled deliberately (e.g. `false && ...`).
7880
not e.(AndLogicalExpr).getAnOperand().(BooleanLiteral).getBooleanValue() = false
7981
select e, "Expression always evaluates to the same value."

java/ql/test/query-tests/ConstantExpAppearsNonConstant/ConstantExpAppearsNonConstant.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
| Test.java:18:33:18:49 | ... * ... | Expression always evaluates to the same value. |
2-
| Test.java:19:34:19:50 | ... * ... | Expression always evaluates to the same value. |
3-
| Test.java:20:29:20:48 | ... * ... | Expression always evaluates to the same value. |
4-
| Test.java:21:32:21:50 | ... * ... | Expression always evaluates to the same value. |
1+
| Test.java:18:33:18:53 | ... * ... | Expression always evaluates to the same value. |
2+
| Test.java:19:34:19:54 | ... * ... | Expression always evaluates to the same value. |
3+
| Test.java:20:29:20:58 | ... * ... | Expression always evaluates to the same value. |
4+
| Test.java:21:32:21:69 | ... * ... | Expression always evaluates to the same value. |
55
| Test.java:27:28:27:44 | ... % ... | Expression always evaluates to the same value. |
66
| Test.java:29:29:29:48 | ... % ... | Expression always evaluates to the same value. |
77
| Test.java:30:32:30:50 | ... % ... | Expression always evaluates to the same value. |

java/ql/test/query-tests/ConstantExpAppearsNonConstant/Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public void tester(){
1515
int mul_constant_left = 0 * 60 * 60 * 24; //OK
1616
int mul_constant_right = 60 * 60 * 24 * 0; //OK
1717
int mul_is_not_constant = rnd.nextInt() * 1; //OK
18-
int mul_is_constant_int_left = 0 * rnd.nextInt(); //NOT OK
19-
int mul_is_constant_int_right = rnd.nextInt() * 0; //NOT OK
20-
long mul_is_constant_hex = rnd.nextLong() * 0x0; //NOT OK
21-
long mul_is_constant_binary = rnd.nextLong() * 00; //NOT OK
22-
18+
int mul_is_constant_int_left = (0+0) * rnd.nextInt(); //NOT OK
19+
int mul_is_constant_int_right = rnd.nextInt() * (1-1); //NOT OK
20+
long mul_is_constant_hex = rnd.nextLong() * (0x0F & 0xF0); //NOT OK
21+
long mul_is_constant_binary = rnd.nextLong() * (0b010101 & 0b101010); //NOT OK
22+
int mul_explicit_zero = rnd.nextInt() * 0; //OK (deliberate zero multiplication)
2323
//Remainder by 1
2424
int rem_not_constant = 42 % 6; //OK
2525
int rem_constant = 60 % 1; //OK

0 commit comments

Comments
 (0)