Skip to content

Commit ab07a38

Browse files
committed
Use ternary type for architecture bit size
1 parent 015519e commit ab07a38

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,29 @@ private int validBitSize() { result = [7, 8, 15, 16, 31, 32, 63, 64] }
156156
private newtype TArchitectureBitSize =
157157
TMk32Bit() or
158158
TMk64Bit() or
159-
TMkArchitectureIndependent()
159+
TMkUnknown()
160+
161+
private class ArchitectureBitSize extends TArchitectureBitSize {
162+
/** Gets an integer for the architecture bit size, if known. */
163+
int toInt() {
164+
this = TMk32Bit() and result = 32
165+
or
166+
this = TMk64Bit() and result = 64
167+
}
168+
169+
/** Holds if the architecture bit size is unknown. */
170+
predicate isUnknown() { this = TMkUnknown() }
171+
172+
/** Gets a textual representation of this element. */
173+
string toString() {
174+
result = this.toInt() + "-bit"
175+
or
176+
this.isUnknown() and result = "unknown"
177+
}
178+
}
160179

161180
private newtype TMaxValueState =
162-
TMkMaxValueState(int bitSize, TArchitectureBitSize architectureBitSize) {
181+
TMkMaxValueState(int bitSize, ArchitectureBitSize architectureBitSize) {
163182
bitSize = validBitSize()
164183
}
165184

@@ -176,15 +195,8 @@ private class MaxValueState extends TMaxValueState {
176195
*/
177196
int getBitSize() { this = TMkMaxValueState(result, _) }
178197

179-
/**
180-
* Gets whether the architecture is 32 bit or 64 bit, if that has been
181-
* determined already.
182-
*/
183-
int getArchitectureBitSize() {
184-
this = TMkMaxValueState(_, TMk32Bit()) and result = 32
185-
or
186-
this = TMkMaxValueState(_, TMk64Bit()) and result = 64
187-
}
198+
/** Gets whether the architecture is 32 bit or 64 bit, or if it is unknown. */
199+
ArchitectureBitSize getArchitectureBitSize() { this = TMkMaxValueState(_, result) }
188200

189201
/**
190202
* Gets the bitsize we should use for a sink.
@@ -201,9 +213,9 @@ private class MaxValueState extends TMaxValueState {
201213
/** Gets a textual representation of this element. */
202214
string toString() {
203215
exists(string suffix |
204-
if exists(this.getArchitectureBitSize())
205-
then suffix = " (on " + this.getArchitectureBitSize() + "-bit architecture)"
206-
else suffix = ""
216+
suffix = " (on " + this.getArchitectureBitSize().toInt() + "-bit architecture)"
217+
or
218+
this.getArchitectureBitSize().isUnknown() and suffix = ""
207219
|
208220
result = "MaxValueState(max value <= 2^(" + this.getBitSize() + ")-1" + suffix
209221
)
@@ -339,9 +351,7 @@ class UpperBoundCheck extends BarrierFlowStateTransformer {
339351
// this will find results that only exist on 32-bit architectures.
340352
not g.isBoundFor(bitsize, state.getSinkBitSize(32))
341353
) and
342-
if exists(state.getArchitectureBitSize())
343-
then result.getArchitectureBitSize() = state.getArchitectureBitSize()
344-
else not exists(result.getArchitectureBitSize())
354+
result.getArchitectureBitSize() = state.getArchitectureBitSize()
345355
}
346356
}
347357

@@ -385,10 +395,10 @@ private module ConversionWithoutBoundsCheckConfig implements DataFlow::StateConf
385395
then
386396
exists(int b | b = [32, 64] |
387397
state.getBitSize() = adjustBitSize(0, sourceIsSigned, b) and
388-
state.getArchitectureBitSize() = b
398+
state.getArchitectureBitSize().toInt() = b
389399
)
390400
else (
391-
not exists(state.getArchitectureBitSize()) and
401+
state.getArchitectureBitSize().isUnknown() and
392402
state.getBitSize() =
393403
min(int bitsize |
394404
bitsize = validBitSize() and
@@ -412,7 +422,7 @@ private module ConversionWithoutBoundsCheckConfig implements DataFlow::StateConf
412422
// Use a default value of 32 for `MaxValueState.getSinkBitSize` because
413423
// this will find results that only exist on 32-bit architectures.
414424
architectureBitSize = getIntTypeBitSize(sink.getFile(), state.getSinkBitSize(32)) and
415-
not (state.getArchitectureBitSize() = 32 and architectureBitSize = 64) and
425+
not (state.getArchitectureBitSize().toInt() = 32 and architectureBitSize = 64) and
416426
sink.getResultType().getUnderlyingType() = integerType and
417427
(
418428
sinkBitsize = integerType.getSize()

0 commit comments

Comments
 (0)