Skip to content

Commit

Permalink
Validate length before turning into an Int.
Browse files Browse the repository at this point in the history
1.x branch version of apple#1375
  • Loading branch information
thomasvl committed Feb 14, 2023
1 parent 7e36981 commit 548c845
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Sources/SwiftProtobuf/BinaryDelimited.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,18 @@ public enum BinaryDelimited {
partial: Bool = false,
options: BinaryDecodingOptions = BinaryDecodingOptions()
) throws {
let length = try Int(decodeVarint(stream))
if length == 0 {
let unsignedLength = try decodeVarint(stream)
if unsignedLength == 0 {
// The message was all defaults, nothing to actually read.
return
}

guard unsignedLength <= Int.max else {
// Due to the trip through an Array below, it has to fit, and Array uses
// Int (signed) for Count.
// Adding a new case is a breaking change, reuse malformedProtobuf.
throw BinaryDecodingError.malformedProtobuf
}
let length = Int(unsignedLength)
var data = Data(count: length)
var bytesRead: Int = 0
data.withUnsafeMutableBytes { (body: UnsafeMutableRawBufferPointer) in
Expand Down

0 comments on commit 548c845

Please sign in to comment.