Skip to content

Commit c796cba

Browse files
committed
Improve UpperBoundCheckGuard.isBoundFor
1 parent 3949914 commit c796cba

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

go/ql/lib/semmle/go/security/IncorrectIntegerConversionLib.qll

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ class UpperBoundCheckGuard extends DataFlow::RelationalComparisonNode {
275275
}
276276

277277
/**
278-
* Gets the constant value which this upper bound check ensures the
279-
* other value is less than or equal to.
278+
* Holds if the upper bound check ensures the non-constant operand is less
279+
* than or equal to the maximum value for `bitSize` and `isSigned`. In this
280+
* case, the upper bound check is a barrier guard.
280281
*/
281282
predicate isBoundFor(int bitSize, boolean isSigned) {
282283
bitSize = [8, 16, 32] and
@@ -288,18 +289,15 @@ class UpperBoundCheckGuard extends DataFlow::RelationalComparisonNode {
288289
then strictnessOffset = 1
289290
else strictnessOffset = 0
290291
|
291-
(
292-
bound = expr.getAnOperand().getExactValue().toFloat()
293-
or
294-
exists(DeclaredConstant maxint | maxint.hasQualifiedName("math", "MaxInt") |
295-
expr.getAnOperand() = maxint.getAReference() and
296-
bound = getMaxIntValue(32, true)
297-
)
298-
or
299-
exists(DeclaredConstant maxuint | maxuint.hasQualifiedName("math", "MaxUint") |
300-
expr.getAnOperand() = maxuint.getAReference() and
301-
bound = getMaxIntValue(32, false)
302-
)
292+
exists(DeclaredConstant maxint, DeclaredConstant maxuint |
293+
maxint.hasQualifiedName("math", "MaxInt") and maxuint.hasQualifiedName("math", "MaxUint")
294+
|
295+
if expr.getAnOperand() = maxint.getAReference()
296+
then bound = getMaxIntValue(32, true)
297+
else
298+
if expr.getAnOperand() = maxuint.getAReference()
299+
then bound = getMaxIntValue(32, false)
300+
else bound = expr.getAnOperand().getExactValue().toFloat()
303301
) and
304302
bound - strictnessOffset <= getMaxIntValue(bitSize, isSigned)
305303
)

0 commit comments

Comments
 (0)