File tree Expand file tree Collapse file tree 3 files changed +24
-10
lines changed
src/Likely Bugs/Arithmetic
test/query-tests/security/CWE-190/semmle/tests Expand file tree Collapse file tree 3 files changed +24
-10
lines changed Original file line number Diff line number Diff line change @@ -27,17 +27,23 @@ class DangerousAssignOpExpr extends AssignOp {
2727
2828predicate problematicCasting ( Type t , Expr e ) { e .getType ( ) .( NumType ) .widerThan ( t ) }
2929
30- Variable getVariable ( DangerousAssignOpExpr a ) {
31- result = a . getDest ( ) .( VarAccess ) .getVariable ( )
30+ Variable getVariable ( Expr dest ) {
31+ result = dest .( VarAccess ) .getVariable ( )
3232 or
33- result = a . getDest ( ) .( ArrayAccess ) .getArray ( ) .( VarAccess ) .getVariable ( )
33+ result = dest .( ArrayAccess ) .getArray ( ) .( VarAccess ) .getVariable ( )
3434}
3535
36- from DangerousAssignOpExpr a , Expr e , Variable v
36+ from DangerousAssignOpExpr a , Expr e , Top v
3737where
3838 e = a .getSource ( ) and
3939 problematicCasting ( a .getDest ( ) .getType ( ) , e ) and
40- v = getVariable ( a )
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+ )
4147select a ,
42- "Implicit cast of source $@ to narrower destination type " + a .getDest ( ) .getType ( ) .getName ( ) + "." ,
43- v , "type " + e .getType ( ) .getName ( )
48+ "Implicit cast of $@ to narrower destination type " + a .getDest ( ) .getType ( ) .getName ( ) + "." ,
49+ v , "source type " + e .getType ( ) .getName ( )
Original file line number Diff line number Diff line change 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- | 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 |
1+ | Test.java:68:5:68:25 | ...+=... | Implicit cast of $@ to narrower destination type int. | Test.java:64:4:64:13 | int i | source type long |
2+ | Test.java:87:4:87:9 | ...+=... | Implicit cast of $@ to narrower destination type int. | Test.java:81:4:81:13 | int i | source type long |
3+ | Test.java:289:5:289:30 | ...+=... | Implicit cast of $@ to narrower destination type int. | Test.java:285:4:285:27 | int[] arr | source type long |
4+ | Test.java:293:7:293:44 | ...+=... | Implicit cast of $@ to narrower destination type int. | Test.java:293:7:293:24 | ...[...] | source type long |
Original file line number Diff line number Diff line change @@ -288,13 +288,20 @@ public static void main(String[] args) {
288288 // which will result in overflows if it is large
289289 arr [2 ] += getLargeNumber ();
290290 }
291+
292+ // BAD.
293+ getAnIntArray ()[0 ] += getLargeNumber ();
291294 }
292295 }
293296
294297 public static long getLargeNumber () {
295298 return Long .MAX_VALUE / 2 ;
296299 }
297300
301+ public static int [] getAnIntArray () {
302+ return new int [10 ];
303+ }
304+
298305 public static boolean properlyBounded (int i ) {
299306 return i < Integer .MAX_VALUE ;
300307 }
You can’t perform that action at this time.
0 commit comments