Skip to content

Commit 44fba68

Browse files
authored
Merge pull request #16128 from erik-krogh/java-info
Java: add link to the source variable in the alert-message for `java/implicit-cast-in-compound-assignment`
2 parents e08790d + 8cb6598 commit 44fba68

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,23 @@ class DangerousAssignOpExpr extends AssignOp {
2727

2828
predicate problematicCasting(Type t, Expr e) { e.getType().(NumType).widerThan(t) }
2929

30-
from DangerousAssignOpExpr a, Expr e
30+
Variable getVariable(Expr dest) {
31+
result = dest.(VarAccess).getVariable()
32+
or
33+
result = dest.(ArrayAccess).getArray().(VarAccess).getVariable()
34+
}
35+
36+
from DangerousAssignOpExpr a, Expr e, Top v
3137
where
3238
e = a.getSource() and
33-
problematicCasting(a.getDest().getType(), e)
39+
problematicCasting(a.getDest().getType(), e) and
40+
(
41+
v = getVariable(a.getDest())
42+
or
43+
// fallback, in case we can't easily determine the variable
44+
not exists(getVariable(a.getDest())) and
45+
v = a.getDest()
46+
)
3447
select a,
35-
"Implicit cast of source type " + e.getType().getName() + " to narrower destination type " +
36-
a.getDest().getType().getName() + "."
48+
"Implicit cast of source type " + e.getType().getName() + " to narrower destination type $@.", v,
49+
a.getDest().getType().getName()
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
| Test.java:68:5:68:25 | ...+=... | Implicit cast of source type long to narrower destination type int. |
2-
| Test.java:87:4:87:9 | ...+=... | Implicit cast of source type long to narrower destination type int. |
1+
| Test.java:68:5:68:25 | ...+=... | Implicit cast of source type long to narrower destination type $@. | Test.java:64:4:64:13 | int i | int |
2+
| Test.java:87:4:87:9 | ...+=... | Implicit cast of source type long to narrower destination type $@. | Test.java:81:4:81:13 | int i | int |
3+
| Test.java:289:5:289:30 | ...+=... | Implicit cast of source type long to narrower destination type $@. | Test.java:285:4:285:27 | int[] arr | int |
4+
| Test.java:293:7:293:44 | ...+=... | Implicit cast of source type long to narrower destination type $@. | Test.java:293:7:293:24 | ...[...] | int |

java/ql/test/query-tests/security/CWE-190/semmle/tests/Test.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,29 @@ public static void main(String[] args) {
279279
// subsequently cast to narrower type int
280280
int widenedThenNarrowed = (int) (data2 + 10L);
281281
}
282+
283+
// InformationLoss
284+
{
285+
int[] arr = new int[10];
286+
while (arr[2] < 1000000) {
287+
// BAD: getLargeNumber is implicitly narrowed to an integer
288+
// which will result in overflows if it is large
289+
arr[2] += getLargeNumber();
290+
}
291+
292+
// BAD.
293+
getAnIntArray()[0] += getLargeNumber();
294+
}
282295
}
283296

284297
public static long getLargeNumber() {
285298
return Long.MAX_VALUE / 2;
286299
}
287300

301+
public static int[] getAnIntArray() {
302+
return new int[10];
303+
}
304+
288305
public static boolean properlyBounded(int i) {
289306
return i < Integer.MAX_VALUE;
290307
}

0 commit comments

Comments
 (0)