Skip to content

Commit ece2713

Browse files
committed
Validate length before turning into an Int.
1.x branch version of #1375
1 parent 7e36981 commit ece2713

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/SwiftProtobuf/BinaryDelimited.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,18 @@ public enum BinaryDelimited {
155155
partial: Bool = false,
156156
options: BinaryDecodingOptions = BinaryDecodingOptions()
157157
) throws {
158-
let length = try Int(decodeVarint(stream))
159-
if length == 0 {
158+
let unsignedLength = try decodeVarint(stream)
159+
if unsignedLength == 0 {
160160
// The message was all defaults, nothing to actually read.
161161
return
162162
}
163-
163+
guard unsignedLength <= Int.max else {
164+
// Due to the trip through an Array below, it has to fit, and Array uses
165+
// Int (signed) for Count.
166+
// Adding a new case is a breaking change, reuse malformedProtobuf.
167+
throw BinaryDecodingError.malformedProtobuf
168+
}
169+
let length = Int(unsignedLength)
164170
var data = Data(count: length)
165171
var bytesRead: Int = 0
166172
data.withUnsafeMutableBytes { (body: UnsafeMutableRawBufferPointer) in

0 commit comments

Comments
 (0)