File tree Expand file tree Collapse file tree 3 files changed +18
-1
lines changed
src/Likely Bugs/Arithmetic
test/query-tests/security/CWE-190/semmle/tests Expand file tree Collapse file tree 3 files changed +18
-1
lines 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
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 () {
You can’t perform that action at this time.
0 commit comments