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 {
2727
2828predicate 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
3137where
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+ )
3447select 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) {
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 }
You can’t perform that action at this time.
0 commit comments