Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6dee706

Browse files
committed
C++: Handle constant variable accesses in SimpleRangeAnalysis.qll
1 parent bbcf0b5 commit 6dee706

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

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

Lines changed: 18 additions & 1 deletion
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.getType().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

0 commit comments

Comments
 (0)