mirror of
https://github.com/apple/swift-nio
synced 2026-11-26 04:57:49 +00:00
ByteBuffer: deal correctly with underreporting ContiguousCollections (#501)
Motivation: ContiguousCollections shouldn't give access to more than `count` bytes via `withUnsafeBytes` but if they do we should still handle this correctly and only copy `count` bytes. Modifications: - copy an upper limit of `count` bytes - fail hard if `withUnsafeBytes` offers fewer bytes as we can't do anything sensible then. Result: even illegal `ContiguousCollection` implementations don't cause issues.
This commit is contained in:
1 parent
e9aa6926ef
commit
072babe443
3 files changed
+59
-4
No files matched your search
@@ -369,18 +369,21 @@ public struct ByteBuffer {
|
||||
|
||||
@_inlineable @_versioned
|
||||
mutating func _set<S: ContiguousCollection>(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
|
||||
|
||||
@@ -124,6 +124,7 @@ extension ByteBufferTest {
|
||||
("testViewsStartIndexIsStable", testViewsStartIndexIsStable),
|
||||
("testSlicesOfByteBufferViewsAreByteBufferViews", testSlicesOfByteBufferViewsAreByteBufferViews),
|
||||
("testReadableBufferViewRangeEqualCapacity", testReadableBufferViewRangeEqualCapacity),
|
||||
("testWeDontWriteTooMuchForUnderreportingContiguousCollection", testWeDontWriteTooMuchForUnderreportingContiguousCollection),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<UInt8>.Index
|
||||
typealias SubSequence = Array<UInt8>.SubSequence
|
||||
typealias Indices = Array<UInt8>.Indices
|
||||
|
||||
public var count: Int {
|
||||
// we're reporting 3 elements
|
||||
return 3
|
||||
}
|
||||
|
||||
public var indices: Indices {
|
||||
return CountableRange(0...2)
|
||||
}
|
||||
|
||||
public subscript(bounds: Range<Index>) -> 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<R>(_ 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 {
|
||||
|
||||
Reference in new issue
Block a user