mirror of
https://github.com/github/codeql.git
synced 2026-04-20 22:44:52 +02:00
also get the variable for array accesses
This commit is contained in:
@@ -27,11 +27,17 @@ class DangerousAssignOpExpr extends AssignOp {
|
||||
|
||||
predicate problematicCasting(Type t, Expr e) { e.getType().(NumType).widerThan(t) }
|
||||
|
||||
Variable getVariable(DangerousAssignOpExpr a) {
|
||||
result = a.getDest().(VarAccess).getVariable()
|
||||
or
|
||||
result = a.getDest().(ArrayAccess).getArray().(VarAccess).getVariable()
|
||||
}
|
||||
|
||||
from DangerousAssignOpExpr a, Expr e, Variable v
|
||||
where
|
||||
e = a.getSource() and
|
||||
problematicCasting(a.getDest().getType(), e) and
|
||||
v = a.getDest().(VarAccess).getVariable()
|
||||
v = getVariable(a)
|
||||
select a,
|
||||
"Implicit cast of source $@ to narrower destination type " + a.getDest().getType().getName() + ".",
|
||||
v, "type " + e.getType().getName()
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
|
||||
@@ -279,6 +279,16 @@ class Test {
|
||||
// subsequently cast to narrower type int
|
||||
int widenedThenNarrowed = (int) (data2 + 10L);
|
||||
}
|
||||
|
||||
// InformationLoss
|
||||
{
|
||||
int[] arr = new int[10];
|
||||
while (arr[2] < 1000000) {
|
||||
// BAD: getLargeNumber is implicitly narrowed to an integer
|
||||
// which will result in overflows if it is large
|
||||
arr[2] += getLargeNumber();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static long getLargeNumber() {
|
||||
|
||||
Reference in New Issue
Block a user