Skip to content

Commit 544fcdd

Browse files
committed
Simplify parseAsBigInt even further
1 parent 5fa02d6 commit 544fcdd

File tree

2 files changed

+21
-58
lines changed

2 files changed

+21
-58
lines changed

cpp/ql/lib/semmle/code/cpp/rangeanalysis/RangeAnalysisUtils.qll

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -485,61 +485,16 @@ QlBuiltins::BigInt varMaxVal(Variable v) { result = typeUpperBound(v.getUnspecif
485485
QlBuiltins::BigInt infinityAsBigInt() { result = 1.toBigInt().bitShiftLeft(1024) }
486486

487487
QlBuiltins::BigInt parseAsBigInt(string s) {
488-
s = any(Expr e).getValue() and
489-
(
490-
result = s.toBigInt()
491-
or
492-
s.toFloat() = 1.0 / 0.0 and result = infinityAsBigInt()
493-
or
494-
s.toFloat() = -(1.0 / 0.0) and result = -infinityAsBigInt()
495-
or
496-
exists(QlBuiltins::BigInt coeff, int base10exp | parseFiniteAsBigInt(s, coeff, base10exp) |
497-
if base10exp < 0
498-
then result = coeff / 10.toBigInt().pow(-base10exp)
499-
else result = coeff * 10.toBigInt().pow(base10exp)
500-
)
501-
)
502-
}
503-
504-
bindingset[s]
505-
private predicate parseFiniteAsBigInt(string s, QlBuiltins::BigInt coeff, int base10exp) {
506-
exists(string t | s = "+" + t | parseUnsignedAsBigInt(t, coeff, base10exp))
507-
or
508-
exists(string t | s = "-" + t | parseUnsignedAsBigInt(t, -coeff, base10exp))
509-
or
510-
parseUnsignedAsBigInt(s, coeff, base10exp)
511-
}
512-
513-
bindingset[s]
514-
private predicate parseUnsignedAsBigInt(string s, QlBuiltins::BigInt coeff, int base10exp) {
515-
exists(string beforeE, int base10expAfterE, int base10expBeforeE |
516-
beforeE = s.toUpperCase().splitAt("E", 0) and
517-
base10expAfterE = parseSignedInt(s.toUpperCase().splitAt("E", 1)) and
518-
parseUnsignedDecimalAsBigInt(beforeE, coeff, base10expBeforeE) and
519-
base10exp = base10expBeforeE + base10expAfterE
520-
)
521-
or
522-
exists(string beforeDot, string afterDot |
523-
beforeDot = s.splitAt(".", 0) and
524-
afterDot = s.splitAt(".", 1) and
525-
coeff = (beforeDot + afterDot).toBigInt() and
526-
base10exp = -afterDot.length()
527-
)
528-
}
529-
530-
bindingset[s]
531-
private predicate parseUnsignedDecimalAsBigInt(string s, QlBuiltins::BigInt coeff, int base10exp) {
532-
exists(string beforeDot, string afterDot |
533-
beforeDot = s.splitAt(".", 0) and
534-
afterDot = s.splitAt(".", 1) and
535-
coeff = (beforeDot + afterDot).toBigInt() and
536-
base10exp = -afterDot.length()
488+
exists(Expr e | s = e.getValue() |
489+
if exists(s.toBigInt()) then
490+
result = s.toBigInt()
491+
else
492+
exists(float f | f = s.toFloat() |
493+
f = 1.0 / 0.0 and result = infinityAsBigInt()
494+
or
495+
f = -(1.0 / 0.0) and result = -infinityAsBigInt()
496+
or
497+
result = f.toString().toBigInt()
498+
)
537499
)
538500
}
539-
540-
bindingset[s]
541-
private int parseSignedInt(string s) {
542-
exists(string t | s = "+" + t | result = t.toInt())
543-
or
544-
result = s.toInt()
545-
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
2+
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
23

3-
from VariableAccess expr
4-
select expr, upperBound(expr).toString()
4+
from VariableAccess expr, QlBuiltins::BigInt u, string s
5+
where
6+
u = upperBound(expr) and
7+
if u = infinityAsBigInt()
8+
then s = "Infinity"
9+
else if u = -infinityAsBigInt()
10+
then s = "-Infinity"
11+
else s = u.toString()
12+
select expr, s

0 commit comments

Comments
 (0)