Throw error when the max read amount is greater than ByteBuffer can tolerate (#2911)

Motivation:

As described in issue
[#2878](https://github.com/apple/swift-nio/issues/2878), NIOFileSystem
crashes when reading more than `ByteBuffer` capacity.

Modifications:

- Add a new static property, `byteBufferCapacity`, to `ByteCount`
representing the maximum amount of bytes that can be written to
`ByteBuffer`.
- Throw a `FileSystemError` in
`ReadableFileHandleProtocol.readToEnd(fromAbsoluteOffset:maximumSizeAllowed:)`
when `maximumSizeAllowed` is more than `ByteCount.byteBufferCapacity`.

Result:

NIOFileSystem will `throw` instead of crashing.
This commit is contained in:
Clinton Nkwocha authored and GitHub committed 2024-10-10 09:32:50 +00:00
1 parent 9ff5fddb3d
commit 611fa094dc
3 files changed
+47 -4

No files matched your search

@@ -1803,6 +1803,21 @@ extension FileSystemTests {
XCTAssertEqual(byteCount, Int(size))
}
}
func testReadMoreThanByteBufferCapacity() async throws {
let path = try await self.fs.temporaryFilePath()
try await self.fs.withFileHandle(forReadingAndWritingAt: path) { fileHandle in
await XCTAssertThrowsFileSystemErrorAsync {
// Set `maximumSizeAllowed` to 1 byte more than can be written to `ByteBuffer`.
try await fileHandle.readToEnd(
maximumSizeAllowed: .byteBufferCapacity + .bytes(1)
)
} onError: { error in
XCTAssertEqual(error.code, .resourceExhausted)
}
}
}
}
#if !canImport(Darwin) && swift(<5.9.2)