-
Notifications
You must be signed in to change notification settings - Fork 652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw error when the max read amount is greater than ByteBuffer
can tolerate
#2911
Throw error when the max read amount is greater than ByteBuffer
can tolerate
#2911
Conversation
Motivation: As described in issue [apple#2878](apple#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.
#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) | ||
private static let byteBufferMaxIndex = UInt32(Int.max) | ||
#else | ||
// on 64-bit platforms we're good | ||
private static let byteBufferMaxIndex = UInt32.max | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually need these static let
s or should we just do this within the body of static var byteBufferCapacity: ByteCount { ... }
?
The maximum size allowed (\(maximumSizeAllowed)) is more than the maximum \ | ||
amount of bytes that can be written to ByteBuffer \ | ||
(\(ByteCount.byteBufferCapacity)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The maximum size allowed (\(maximumSizeAllowed)) is more than the maximum \ | |
amount of bytes that can be written to ByteBuffer \ | |
(\(ByteCount.byteBufferCapacity)). | |
The maximum size allowed (\(maximumSizeAllowed)) is more than the maximum \ | |
amount of bytes that can be written to ByteBuffer \ | |
(\(ByteCount.byteBufferCapacity)). You can read the file in smaller chunks by \ | |
calling readChunks(). |
/// an offset other than zero is passed. Also, it will throw | ||
/// ``FileSystemError/Code-swift.struct/resourceExhausted`` if `maximumSizeAllowed` is more than can be | ||
/// written to `ByteBuffer`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This past should go in the Throws:
section below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thank you @clintonpi!
ByteBuffer
capacityByteBuffer
can tolerate
Motivation:
As described in issue #2878, NIOFileSystem crashes when reading more than
ByteBuffer
capacity.Modifications:
byteBufferCapacity
, toByteCount
representing the maximum amount of bytes that can be written toByteBuffer
.FileSystemError
inReadableFileHandleProtocol.readToEnd(fromAbsoluteOffset:maximumSizeAllowed:)
whenmaximumSizeAllowed
is more thanByteCount.byteBufferCapacity
.Result:
NIOFileSystem will
throw
instead of crashing.