Skip to content

Commit 015519e

Browse files
committed
Combine isBoundFor and isBoundFor2
1 parent cd40663 commit 015519e

File tree

1 file changed

+4
-37
lines changed

1 file changed

+4
-37
lines changed

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

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ deprecated class ConversionWithoutBoundsCheckConfig extends TaintTracking::Confi
141141
if sinkBitSize != 0 then bitSize = sinkBitSize else bitSize = 32
142142
|
143143
node = DataFlow::BarrierGuard<upperBoundCheckGuard/3>::getABarrierNodeForGuard(g) and
144-
g.isBoundFor(bitSize, sinkIsSigned)
144+
if sinkIsSigned = true then g.isBoundFor(bitSize, 32) else g.isBoundFor(bitSize - 1, 32)
145145
)
146146
or
147147
exists(int bitSize |
@@ -245,39 +245,6 @@ class UpperBoundCheckGuard extends DataFlow::RelationalComparisonNode {
245245
expr.getAnOperand().getType().getUnderlyingType() instanceof IntegerType
246246
}
247247

248-
/**
249-
* Holds if the upper bound check ensures the non-constant operand is less
250-
* than or equal to the maximum value for `bitSize` and `isSigned`. In this
251-
* case, the upper bound check is a barrier guard.
252-
*
253-
* Note that we have to use floats here because integers in CodeQL are
254-
* represented by 32-bit signed integers, which cannot represent some of the
255-
* integer values which we will encounter.
256-
*/
257-
deprecated predicate isBoundFor(int bitSize, boolean isSigned) {
258-
bitSize = [8, 16, 32] and
259-
exists(float bound, float strictnessOffset |
260-
// For `x < c` the bound is `c-1`. For `x >= c` we will be an upper bound
261-
// on the `branch` argument of `checks` is false, which is equivalent to
262-
// `x < c`.
263-
if expr instanceof LssExpr or expr instanceof GeqExpr
264-
then strictnessOffset = 1
265-
else strictnessOffset = 0
266-
|
267-
exists(DeclaredConstant maxint, DeclaredConstant maxuint |
268-
maxint.hasQualifiedName("math", "MaxInt") and maxuint.hasQualifiedName("math", "MaxUint")
269-
|
270-
if expr.getAnOperand() = maxint.getAReference()
271-
then bound = getMaxIntValue(32, true)
272-
else
273-
if expr.getAnOperand() = maxuint.getAReference()
274-
then bound = getMaxIntValue(32, false)
275-
else bound = expr.getAnOperand().getExactValue().toFloat()
276-
) and
277-
bound - strictnessOffset <= getMaxIntValue(bitSize, isSigned)
278-
)
279-
}
280-
281248
/**
282249
* Holds if this upper bound check ensures the non-constant operand is less
283250
* than or equal to `2^(bitsize) - 1`. In this case, the upper bound check
@@ -288,7 +255,7 @@ class UpperBoundCheckGuard extends DataFlow::RelationalComparisonNode {
288255
* represented by 32-bit signed integers, which cannot represent some of the
289256
* integer values which we will encounter.
290257
*/
291-
predicate isBoundFor2(int bitSize, int architectureBitSize) {
258+
predicate isBoundFor(int bitSize, int architectureBitSize) {
292259
bitSize = validBitSize() and
293260
architectureBitSize = [32, 64] and
294261
exists(float bound, float strictnessOffset |
@@ -359,7 +326,7 @@ class UpperBoundCheck extends BarrierFlowStateTransformer {
359326
override predicate barrierFor(MaxValueState flowstate) {
360327
// Use a default value of 32 for `MaxValueState.getSinkBitSize` because
361328
// this will find results that only exist on 32-bit architectures.
362-
g.isBoundFor2(flowstate.getBitSize(), flowstate.getSinkBitSize(32))
329+
g.isBoundFor(flowstate.getBitSize(), flowstate.getSinkBitSize(32))
363330
}
364331

365332
override MaxValueState transform(MaxValueState state) {
@@ -370,7 +337,7 @@ class UpperBoundCheck extends BarrierFlowStateTransformer {
370337
bitsize < state.getBitSize() and
371338
// Use a default value of 32 for `MaxValueState.getSinkBitSize` because
372339
// this will find results that only exist on 32-bit architectures.
373-
not g.isBoundFor2(bitsize, state.getSinkBitSize(32))
340+
not g.isBoundFor(bitsize, state.getSinkBitSize(32))
374341
) and
375342
if exists(state.getArchitectureBitSize())
376343
then result.getArchitectureBitSize() = state.getArchitectureBitSize()

0 commit comments

Comments
 (0)