diff --git a/Sources/NIO/ByteBuffer-core.swift b/Sources/NIO/ByteBuffer-core.swift index 1b78c1cbb..e56ea4a2d 100644 --- a/Sources/NIO/ByteBuffer-core.swift +++ b/Sources/NIO/ByteBuffer-core.swift @@ -369,18 +369,21 @@ public struct ByteBuffer { @_inlineable @_versioned mutating func _set(bytes: S, at index: _Index) -> _Capacity where S.Element == UInt8 { - let newEndIndex: _Index = index + _toIndex(Int(bytes.count)) + let bytesCount = bytes.count + let newEndIndex: _Index = index + _toIndex(Int(bytesCount)) if !isKnownUniquelyReferenced(&self._storage) { let extraCapacity = newEndIndex > self._slice.upperBound ? newEndIndex - self._slice.upperBound : 0 self._copyStorageAndRebase(extraCapacity: extraCapacity) } - self._ensureAvailableCapacity(_Capacity(bytes.count), at: index) + self._ensureAvailableCapacity(_Capacity(bytesCount), at: index) let base = self._storage.bytes.advanced(by: Int(self._slice.lowerBound + index)).assumingMemoryBound(to: UInt8.self) bytes.withUnsafeBytes { srcPtr in - base.assign(from: srcPtr.baseAddress!.assumingMemoryBound(to: S.Element.self), count: srcPtr.count) + precondition(srcPtr.count >= bytesCount, + "collection \(bytes) claims count \(bytesCount) but withUnsafeBytes only offers \(srcPtr.count) bytes") + base.assign(from: srcPtr.baseAddress!.assumingMemoryBound(to: S.Element.self), count: Int(bytesCount)) } - return _toCapacity(Int(bytes.count)) + return _toCapacity(Int(bytesCount)) } @_inlineable @_versioned diff --git a/Tests/NIOTests/ByteBufferTest+XCTest.swift b/Tests/NIOTests/ByteBufferTest+XCTest.swift index 34b612e45..493196ce3 100644 --- a/Tests/NIOTests/ByteBufferTest+XCTest.swift +++ b/Tests/NIOTests/ByteBufferTest+XCTest.swift @@ -124,6 +124,7 @@ extension ByteBufferTest { ("testViewsStartIndexIsStable", testViewsStartIndexIsStable), ("testSlicesOfByteBufferViewsAreByteBufferViews", testSlicesOfByteBufferViewsAreByteBufferViews), ("testReadableBufferViewRangeEqualCapacity", testReadableBufferViewRangeEqualCapacity), + ("testWeDontWriteTooMuchForUnderreportingContiguousCollection", testWeDontWriteTooMuchForUnderreportingContiguousCollection), ] } } diff --git a/Tests/NIOTests/ByteBufferTest.swift b/Tests/NIOTests/ByteBufferTest.swift index 9b2bb54c3..aed254607 100644 --- a/Tests/NIOTests/ByteBufferTest.swift +++ b/Tests/NIOTests/ByteBufferTest.swift @@ -1445,6 +1445,57 @@ class ByteBufferTest: XCTestCase { let viewSlice: ByteBufferView = view[view.startIndex ..< view.endIndex] XCTAssertEqual(buf.readableBytes, viewSlice.count) } + + func testWeDontWriteTooMuchForUnderreportingContiguousCollection() throws { + // this is an illegal contiguous collection but we should still be able to deal with this + struct UnderreportingContiguousCollection: ContiguousCollection { + let storage: [UInt8] = Array(repeating: 0xff, count: 4096) + typealias Element = UInt8 + typealias Index = Array.Index + typealias SubSequence = Array.SubSequence + typealias Indices = Array.Indices + + public var count: Int { + // we're reporting 3 elements + return 3 + } + + public var indices: Indices { + return CountableRange(0...2) + } + + public subscript(bounds: Range) -> SubSequence { + return self.storage[bounds] + } + + public subscript(position: Index) -> Element { + /* this is wrong but we need to check that we don't access this */ + XCTFail("shouldn't have been called") + return 0xff + } + + public var startIndex: Index { + return self.storage.startIndex + } + + public var endIndex: Index { + return self.storage.endIndex + } + + func index(after i: Index) -> Index { + return self.storage.index(after: i) + } + + func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { + // we're giving access to 4096 elements despite the fact we claim to only have 3 available + return try self.storage.withUnsafeBytes(body) + } + } + buf.clear() + buf.write(bytes: UnderreportingContiguousCollection()) + XCTAssertEqual(3, buf.readableBytes) + XCTAssertEqual([0xff, 0xff, 0xff], buf.readBytes(length: buf.readableBytes)!) + } } private enum AllocationExpectationState: Int {