From 71dcee80bdf8c90c98f288371905237ea33fb758 Mon Sep 17 00:00:00 2001 From: Kevin Wooten Date: Mon, 16 Jun 2025 14:54:47 -0700 Subject: [PATCH] CBOR: Use Data.startIndex to ensure correct offset for slices --- Sources/PotentCBOR/CBORStream.swift | 5 +++-- Tests/CBORDecoderTests.swift | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Sources/PotentCBOR/CBORStream.swift b/Sources/PotentCBOR/CBORStream.swift index d0a72a359..fc07212d6 100644 --- a/Sources/PotentCBOR/CBORStream.swift +++ b/Sources/PotentCBOR/CBORStream.swift @@ -62,15 +62,16 @@ public class CBORDataStream: CBORInputStream, CBOROutputStream { public init(data: Data = Data(), offset: Int = 0) { self.data = data - self.offset = offset + self.offset = offset + data.startIndex } public func reset() { data.removeAll() + offset = data.startIndex } func checkAvailable(count: Int) throws { - if (offset + count) > data.count { + if (offset + count) > data.endIndex { throw CBORSerialization.Error.unexpectedEndOfStream } } diff --git a/Tests/CBORDecoderTests.swift b/Tests/CBORDecoderTests.swift index 0281f6e40..7b191b0df 100644 --- a/Tests/CBORDecoderTests.swift +++ b/Tests/CBORDecoderTests.swift @@ -16,6 +16,12 @@ import XCTest class CBORDecoderTests: XCTestCase { + func testDecodeFromSlice() { + let data = Data([0x1, 0x2, 0x3, 0x64, 0x49, 0x45, 0x54, 0x46, 0x3, 0x2, 0x1]) + let slice = data[3..