Skip to content

Commit ed97be4

Browse files
authored
Merge pull request #2454 from aschackmull/java/explicit-mul-zero
Java: Allow explicit zero multiplication in java/evaluation-to-constant.
2 parents 0012fef + 5a2ed9f commit ed97be4

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

change-notes/1.24/analysis-java.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Improvements to Java analysis
2+
3+
The following changes in version 1.24 affect Java analysis in all applications.
4+
5+
## New queries
6+
7+
| **Query** | **Tags** | **Purpose** |
8+
|-----------------------------|-----------|--------------------------------------------------------------------|
9+
10+
## Changes to existing queries
11+
12+
| **Query** | **Expected impact** | **Change** |
13+
|------------------------------|------------------------|-----------------------------------|
14+
| Expression always evaluates to the same value (`java/evaluation-to-constant`) | Fewer false positives | Expressions of the form `0 * x` are usually intended and no longer reported. |
15+
16+
## Changes to libraries
17+

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)