Skip to content

Commit ee32a9f

Browse files
committed
Document the Safety Contract for lastRegularOperandIndex
1 parent 9a3edfd commit ee32a9f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Sources/TSCUtility/BitstreamReader.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ private struct BitstreamReader {
104104
guard numOps > 0 else { throw Error.invalidAbbrev }
105105

106106
var operands: [Bitstream.Abbreviation.Operand] = []
107+
operands.reserveCapacity(numOps)
107108
for i in 0..<numOps {
108109
operands.append(try readAbbrevOp())
109110

@@ -156,7 +157,11 @@ private struct BitstreamReader {
156157
let lastOperand = abbrev.operands.last!
157158
let lastRegularOperandIndex: Int = abbrev.operands.endIndex - (lastOperand.isPayload ? 1 : 0)
158159

159-
// Safety: `lastRegularOperandIndex` is always at least 1.
160+
// Safety: `lastRegularOperandIndex` is always at least 1. An abbreviation
161+
// is required by the format to contain at least one operand. If that last
162+
// operand is a payload (and thus we subtracted one from the total number of
163+
// operands above), then that must mean it is either a trailing array
164+
// or trailing blob. Both of these are preceded by their length field.
160165
let fields = UnsafeMutableBufferPointer<UInt64>.allocate(capacity: lastRegularOperandIndex - 1)
161166
defer { fields.deallocate() }
162167

0 commit comments

Comments
 (0)