File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed
test/query-tests/Security/CWE/CWE-190/semmle/uncontrolled Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,11 @@ import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
1919import TaintedWithPath
2020
2121predicate isUnboundedRandCall ( FunctionCall fc ) {
22- fc .getTarget ( ) .getName ( ) = "rand" and not bounded ( fc )
22+ exists ( Function func | func = fc .getTarget ( ) |
23+ func .hasGlobalOrStdOrBslName ( "rand" ) and
24+ not bounded ( fc ) and
25+ func .getNumberOfParameters ( ) = 0
26+ )
2327}
2428
2529/**
@@ -84,6 +88,10 @@ predicate bounded(Expr e) {
8488 boundedDiv ( e , any ( DivExpr div ) .getLeftOperand ( ) )
8589 or
8690 boundedDiv ( e , any ( AssignDivExpr div ) .getLValue ( ) )
91+ or
92+ boundedDiv ( e , any ( RShiftExpr shift ) .getLeftOperand ( ) )
93+ or
94+ boundedDiv ( e , any ( AssignRShiftExpr div ) .getLValue ( ) )
8795}
8896
8997predicate isUnboundedRandCallOrParent ( Expr e ) {
Original file line number Diff line number Diff line change @@ -37,3 +37,14 @@ void randomTester2()
3737 r = r + 100 ; // BAD
3838 }
3939}
40+
41+ int rand (int min, int max);
42+ unsigned rand (int max);
43+
44+ void test_with_bounded_randomness () {
45+ int r = rand (0 , 10 );
46+ r++; // GOOD
47+
48+ unsigned unsigned_r = rand (10 );
49+ unsigned_r++; // GOOD
50+ }
You can’t perform that action at this time.
0 commit comments