Skip to content

Commit 2261085

Browse files
authored
Merge pull request github#5973 from MathiasVP/more-uncontrolled-arith-improvements
C++: More `cpp/uncontrolled-arithmetic` improvements
2 parents 6d7b95c + 8765c33 commit 2261085

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

cpp/ql/src/Security/CWE/CWE-190/ArithmeticUncontrolled.ql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
1919
import TaintedWithPath
2020

2121
predicate 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

8997
predicate isUnboundedRandCallOrParent(Expr e) {

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/uncontrolled/test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)