File tree Expand file tree Collapse file tree 3 files changed +38
-6
lines changed
src/Likely Bugs/Arithmetic
test/query-tests/security/CWE-190/semmle/tests Expand file tree Collapse file tree 3 files changed +38
-6
lines changed Original file line number Diff line number Diff line change @@ -27,10 +27,23 @@ class DangerousAssignOpExpr extends AssignOp {
27
27
28
28
predicate problematicCasting ( Type t , Expr e ) { e .getType ( ) .( NumType ) .widerThan ( t ) }
29
29
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
31
37
where
32
38
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
+ )
34
47
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 ( )
Original file line number Diff line number Diff line change 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 |
Original file line number Diff line number Diff line change @@ -279,12 +279,29 @@ 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
+
292
+ // BAD.
293
+ getAnIntArray ()[0 ] += getLargeNumber ();
294
+ }
282
295
}
283
296
284
297
public static long getLargeNumber () {
285
298
return Long .MAX_VALUE / 2 ;
286
299
}
287
300
301
+ public static int [] getAnIntArray () {
302
+ return new int [10 ];
303
+ }
304
+
288
305
public static boolean properlyBounded (int i ) {
289
306
return i < Integer .MAX_VALUE ;
290
307
}
You can’t perform that action at this time.
0 commit comments