Skip to content

Commit 28a9bab

Browse files
authored
Merge pull request github#3026 from MathiasVP/simplerangeanalysis-const-var-access
C++: Handle constant variable accesses in SimpleRangeAnalysis.qll
2 parents 5e62f54 + 8b467eb commit 28a9bab

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

cpp/ql/src/semmle/code/cpp/rangeanalysis/SimpleRangeAnalysis.qll

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,30 @@ private float wideningUpperBounds(ArithmeticType t) {
9191
result = 1.0 / 0.0 // +Inf
9292
}
9393

94+
/**
95+
* Gets the value of the expression `e`, if it is a constant.
96+
* This predicate also handles the case of constant variables initialized in compilation units,
97+
* which doesn't necessarily have a getValue() result from the extractor.
98+
*/
99+
private string getValue(Expr e) {
100+
if exists(e.getValue())
101+
then result = e.getValue()
102+
else
103+
exists(VariableAccess access, Variable v |
104+
e = access and
105+
v = access.getTarget() and
106+
v.getUnderlyingType().isConst() and
107+
result = getValue(v.getAnAssignedValue())
108+
)
109+
}
110+
94111
/** Set of expressions which we know how to analyze. */
95112
private predicate analyzableExpr(Expr e) {
96113
// The type of the expression must be arithmetic. We reuse the logic in
97114
// `exprMinVal` to check this.
98115
exists(exprMinVal(e)) and
99116
(
100-
exists(e.getValue().toFloat()) or
117+
exists(getValue(e).toFloat()) or
101118
e instanceof UnaryPlusExpr or
102119
e instanceof UnaryMinusExpr or
103120
e instanceof MinExpr or
@@ -365,8 +382,8 @@ private float getTruncatedLowerBounds(Expr expr) {
365382
then
366383
// If the expression evaluates to a constant, then there is no
367384
// need to call getLowerBoundsImpl.
368-
if exists(expr.getValue().toFloat())
369-
then result = expr.getValue().toFloat()
385+
if exists(getValue(expr).toFloat())
386+
then result = getValue(expr).toFloat()
370387
else (
371388
// Some of the bounds computed by getLowerBoundsImpl might
372389
// overflow, so we replace invalid bounds with exprMinVal.
@@ -418,8 +435,8 @@ private float getTruncatedUpperBounds(Expr expr) {
418435
then
419436
// If the expression evaluates to a constant, then there is no
420437
// need to call getUpperBoundsImpl.
421-
if exists(expr.getValue().toFloat())
422-
then result = expr.getValue().toFloat()
438+
if exists(getValue(expr).toFloat())
439+
then result = getValue(expr).toFloat()
423440
else (
424441
// Some of the bounds computed by `getUpperBoundsImpl`
425442
// might overflow, so we replace invalid bounds with

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ void test10(int x) {
6969
} while (0);
7070
}
7171
}
72+
73+
extern const int const256;
74+
75+
void test11() {
76+
short s;
77+
for(s = 0; s < const256; ++s) {}
78+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const int const256 = 256;

0 commit comments

Comments
 (0)