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
8 changes: 5 additions & 3 deletions Sources/CBORDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ public class CBORDecoder {
}

private func readN(_ n: Int) throws -> [CBOR] {
return try (0..<n).map { _ in
guard let r = try decodeItem() else { throw CBORError.unfinishedSequence }
return r
var result: [CBOR] = []
for _ in 0..<n {
guard let item = try decodeItem() else { throw CBORError.unfinishedSequence }
result.append(item)
}
return result
}

func readUntilBreak() throws -> [CBOR] {
Expand Down
19 changes: 19 additions & 0 deletions Tests/CBORDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ class CBORDecoderTests: XCTestCase {
}
}

func testLargeIncorrectUTF8StringShouldThrowNotCrash() throws {
// https://github.com/valpackett/SwiftCBOR/issues/118
let bytes: [UInt8] = [
0x9b,
0x54, 0x68, 0x47, 0x9e, 0x98, 0x41, 0xd5, 0xed,
0xae, 0x42, 0x4b, 0x9e, 0x68, 0x68, 0x47,
0xa0, 0xf0, 0x41, 0xe2, 0xc4, 0x73, 0x42, 0x4f, 0x8c, 0x48, 0x68, 0x47, 0xa3, 0x48, 0x41, 0xe2,
0x6c, 0xf2, 0x42, 0x3f, 0x1b, 0x3c, 0x68, 0x47, 0xa5, 0xa0, 0x41, 0xe1, 0xce, 0x5a, 0x42, 0x3d,
0x71, 0x74, 0x68, 0x47, 0xa7, 0xf8, 0x41, 0xe2, 0x57, 0x12, 0x42, 0x3b, 0x54, 0x6c, 0x68, 0x47,
0xaa, 0x50, 0x41, 0xe4, 0x17, 0x84, 0x42, 0x40, 0x6d, 0x24, 0x68, 0x47, 0xac, 0xa8, 0x41, 0xe0,
0xa1, 0x91, 0x42, 0x41, 0xef, 0xdc, 0x68, 0x47, 0xaf, 0x0, 0x41, 0xd8, 0x93, 0xd0, 0x42, 0x48,
0xfa, 0xa0, 0x68, 0x47, 0xb1, 0x58, 0x41, 0xd5, 0x5a, 0x5, 0x42, 0x4e, 0xfd, 0xb4, 0x68, 0x47,
0xb3, 0xb0, 0x41, 0xd4, 0x43, 0x1c, 0x42, 0x50, 0x57, 0x6c, 0x68, 0x47, 0xb6, 0x8, 0x41, 0xd3
]
XCTAssertThrowsError(try CBOR.decode(bytes)) { error in
XCTAssertEqual(error as? CBORError, CBORError.incorrectUTF8String)
}
}

func testDecodeMapFromIssue29() {
let loremIpsumData: [UInt8] = [0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74, 0x2c, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c, 0x69, 0x74, 0x2e, 0x20, 0x51, 0x75, 0x69, 0x73, 0x71, 0x75, 0x65, 0x20, 0x65, 0x78, 0x20, 0x61, 0x6e, 0x74, 0x65, 0x2c, 0x20, 0x73, 0x65, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x75, 0x74, 0x20, 0x66, 0x61, 0x75, 0x63, 0x69, 0x62, 0x75, 0x73, 0x20, 0x70, 0x68, 0x61, 0x72, 0x65, 0x74, 0x72, 0x61, 0x2c, 0x20, 0x61, 0x63, 0x63, 0x75, 0x6d, 0x73, 0x61, 0x6e, 0x20, 0x65, 0x74, 0x20, 0x61, 0x75, 0x67, 0x75, 0x65, 0x2e, 0x20, 0x56, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6c, 0x75, 0x6d, 0x20, 0x76, 0x75, 0x6c, 0x70, 0x75, 0x74, 0x61, 0x74, 0x65, 0x20, 0x65, 0x6c, 0x69, 0x74, 0x20, 0x6c, 0x69, 0x67, 0x75, 0x6c, 0x61, 0x2c, 0x20, 0x65, 0x75, 0x20, 0x74, 0x69, 0x6e, 0x63, 0x69, 0x64, 0x75, 0x6e, 0x74, 0x20, 0x6f, 0x72, 0x63, 0x69, 0x20, 0x6c, 0x61, 0x63, 0x69, 0x6e, 0x69, 0x61, 0x20, 0x71, 0x75, 0x69, 0x73, 0x2e, 0x20, 0x50, 0x72, 0x6f, 0x69, 0x6e, 0x20, 0x73, 0x63, 0x65, 0x6c, 0x65, 0x72, 0x69, 0x73, 0x71, 0x75, 0x65, 0x20, 0x64, 0x75, 0x69, 0x20, 0x61, 0x74, 0x20, 0x6d, 0x61, 0x67, 0x6e, 0x61, 0x20, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x72, 0x61, 0x74, 0x2c, 0x20, 0x69, 0x64, 0x20, 0x62, 0x6c, 0x61, 0x6e, 0x64, 0x69, 0x74, 0x20, 0x66, 0x65, 0x6c, 0x69, 0x73, 0x20, 0x76, 0x65, 0x68, 0x69, 0x63, 0x75, 0x6c, 0x61, 0x2e, 0x20, 0x4d, 0x61, 0x65, 0x63, 0x65, 0x6e, 0x61, 0x73, 0x20, 0x61, 0x63, 0x20, 0x6e, 0x69, 0x73, 0x6c, 0x20, 0x61, 0x20, 0x6f, 0x64, 0x69, 0x6f, 0x20, 0x76, 0x61, 0x72, 0x69, 0x75, 0x73, 0x20, 0x63, 0x6f, 0x6e, 0x64, 0x69, 0x6d, 0x65, 0x6e, 0x74, 0x75, 0x6d, 0x20, 0x6c]
let data: [UInt8] = [0xbf, 0x63, 0x6f, 0x66, 0x66, 0x00, 0x64, 0x64, 0x61, 0x74, 0x61, 0x59, 0x01, 0x2c]
Expand Down