| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 795b767 commit 8b220cc
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,11 +27,17 @@ class DangerousAssignOpExpr extends AssignOp { | |||
| 27 | 27 | ||
| 28 | 28 | predicate problematicCasting(Type t, Expr e) { e.getType().(NumType).widerThan(t) } | |
| 29 | 29 | ||
| 30 | + Variable getVariable(DangerousAssignOpExpr a) { | ||
| 31 | + result = a.getDest().(VarAccess).getVariable() | ||
| 32 | + or | ||
| 33 | + result = a.getDest().(ArrayAccess).getArray().(VarAccess).getVariable() | ||
| 34 | + } | ||
| 35 | + | ||
| 30 | 36 | from DangerousAssignOpExpr a, Expr e, Variable v | |
| 31 | 37 | where | |
| 32 | 38 | e = a.getSource() and | |
| 33 | 39 | problematicCasting(a.getDest().getType(), e) and | |
| 34 | - v = a.getDest().(VarAccess).getVariable() | ||
| 40 | + v = getVariable(a) | ||
| 35 | 41 | select a, | |
| 36 | 42 | "Implicit cast of source $@ to narrower destination type " + a.getDest().getType().getName() + ".", | |
| 37 | 43 | v, "type " + e.getType().getName() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,2 +1,3 @@ | |||
| 1 | 1 | | Test.java:68:5:68:25 | ...+=... | Implicit cast of source $@ to narrower destination type int. | Test.java:64:4:64:13 | int i | type long | | |
| 2 | 2 | | Test.java:87:4:87:9 | ...+=... | Implicit cast of source $@ to narrower destination type int. | Test.java:81:4:81:13 | int i | type long | | |
| 3 | + | Test.java:289:5:289:30 | ...+=... | Implicit cast of source $@ to narrower destination type int. | Test.java:285:4:285:27 | int[] arr | type long | | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -279,6 +279,16 @@ public static void main(String[] args) { | |||
| 279 | 279 | // subsequently cast to narrower type int | |
| 280 | 280 | int widenedThenNarrowed = (int) (data2 + 10L); | |
| 281 | 281 | } | |
| 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 | + } | ||
| 282 | 292 | } | |
| 283 | 293 | ||
| 284 | 294 | public static long getLargeNumber() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments