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

+15
View File
@@ -80,6 +80,21 @@ public struct ByteCount: Hashable, Sendable {
}
}
extension ByteCount {
/// A ``ByteCount`` for the maximum amount of bytes that can be written to `ByteBuffer`.
internal static var byteBufferCapacity: ByteCount {
#if arch(arm) || arch(i386) || arch(arm64_32)
// on 32-bit platforms we can't make use of a whole UInt32.max (as it doesn't fit in an Int)
let byteBufferMaxIndex = UInt32(Int.max)
#else
// on 64-bit platforms we're good
let byteBufferMaxIndex = UInt32.max
#endif
return ByteCount(bytes: Int64(byteBufferMaxIndex))
}
}
extension ByteCount: AdditiveArithmetic {
public static var zero: ByteCount { ByteCount(bytes: 0) }