diff --git a/Sources/CBORCoding/CBORParser.swift b/Sources/CBORCoding/CBORParser.swift index c7f7c0d..52a20da 100644 --- a/Sources/CBORCoding/CBORParser.swift +++ b/Sources/CBORCoding/CBORParser.swift @@ -20,10 +20,10 @@ internal class CBORParser { guard !data.isEmpty else { return nil } var storage = Storage() - var index: Int = 0 + var index: Int = data.startIndex do { - while index < data.count { + while index < data.endIndex { let majorType = CBOR.majorType(for: data[index]) switch majorType { diff --git a/Tests/CBORCodingTests/CBORParserTests.swift b/Tests/CBORCodingTests/CBORParserTests.swift index 5043561..7ef8418 100644 --- a/Tests/CBORCodingTests/CBORParserTests.swift +++ b/Tests/CBORCodingTests/CBORParserTests.swift @@ -1075,6 +1075,18 @@ class CBORParserTests: XCTestCase { XCTAssertThrowsError(try CBORParser.testCreateCodingKey(from: UInt64.max)) } + + func testSlicedDataInput() { + var value: Any! + + // Skip over the first 2 bytes with a slice. + let data = convertFromHexString("0x000000")[2...] + + XCTAssertNoThrow(value = try CBORParser.parse(data)) + XCTAssertTrue(value is UInt64) + XCTAssertEqual(value as! UInt64, 0) + + } // MARK: Private Methods