Skip to content

Fix exception when parsing with sliced Data #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 19, 2021
Merged
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
4 changes: 2 additions & 2 deletions Sources/CBORCoding/CBORParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions Tests/CBORCodingTests/CBORParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down