Skip to content

Commit 3f0bfe1

Browse files
committed
C++: Remove the implicit assumption about the existence of a lower bound implying the existence of an upper bound (and vice veraa).
1 parent 63f50a9 commit 3f0bfe1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

cpp/ql/lib/semmle/code/cpp/commons/Printf.qll

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,22 +1069,25 @@ class FormatLiteral extends Literal {
10691069
or
10701070
// The second case uses range analysis to deduce a length that's shorter than the length
10711071
// of the number -2^31.
1072-
exists(Expr arg, float lower |
1072+
exists(Expr arg, float lower, float upper |
10731073
arg = this.getUse().getConversionArgument(n) and
1074-
lower = lowerBound(arg.getFullyConverted())
1074+
lower = lowerBound(arg.getFullyConverted()) and
1075+
upper = upperBound(arg.getFullyConverted())
10751076
|
10761077
cand =
10771078
max(int cand0 |
1079+
// Include the sign bit in the length if it can be negative
10781080
(
1079-
// Include the sign bit in the length of `lower` if it can be negative
10801081
if lower < 0
10811082
then cand0 = 1 + lengthInBase10(lower.abs())
10821083
else cand0 = lengthInBase10(lower)
10831084
)
10841085
or
1085-
// We don't care about the sign of `upper`: if `upper` is negative, then we know
1086-
// `lower` is also (possibly more) negative, and thus its length will be greater.
1087-
cand0 = lengthInBase10(upperBound(arg.getFullyConverted()))
1086+
(
1087+
if upper < 0
1088+
then cand0 = 1 + lengthInBase10(upper.abs())
1089+
else cand0 = lengthInBase10(upper)
1090+
)
10881091
)
10891092
)
10901093
)

0 commit comments

Comments
 (0)