Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/MessagePackTypes/MessagePackType+DoubleType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ extension MessagePackType.DoubleType {
}

static func unpack(for value: Data) throws -> Double {
guard let firstByte = value.first else { throw MessagePackError.emptyData }
guard firstByte == self.firstByte else { throw MessagePackError.invalidData }

let unpacked: UInt64 = try unpackInteger(try value.subdata(dataRange))
return Double(bitPattern: UInt64(bigEndian: unpacked))
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/MessagePackTypes/MessagePackType+FloatType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ extension MessagePackType.FloatType {
}

static func unpack(for value: Data) throws -> Float {
guard let firstByte = value.first else { throw MessagePackError.emptyData }
guard firstByte == self.firstByte else { throw MessagePackError.invalidData }

let unpacked: UInt32 = try unpackInteger(try value.subdata(dataRange))
return Float(bitPattern: UInt32(bigEndian: unpacked))
}
Expand Down
5 changes: 5 additions & 0 deletions Tests/MessagePackerTests/DoubleUnpackedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ class DoubleUnpackedTests: XCTestCase {
let output = 3.14
XCTAssertEqual(try decoder.decode(Double.self, from: input), output)
}

func testMismatchedType() {
let input = Data([207, 255, 255, 255, 255, 255, 255, 255, 255])
XCTAssertThrowsError(try decoder.decode(Double.self, from: input))
}
}
5 changes: 5 additions & 0 deletions Tests/MessagePackerTests/FloatUnpackedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ class FloatUnpackedTests: XCTestCase {
let output: Float = 3.14
XCTAssertEqual(try decoder.decode(Float.self, from: input), output)
}

func testMismatchedType() {
let input = Data([207, 255, 255, 255, 255, 255, 255, 255, 255])
XCTAssertThrowsError(try decoder.decode(Float.self, from: input))
}
}