Skip to content
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

Conversation

clintonpi
Copy link
Contributor

Motivation:

As described in issue #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.

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.
Comment on lines 84 to 90
#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
Copy link
Contributor

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 lets or should we just do this within the body of static var byteBufferCapacity: ByteCount { ... }?

Comment on lines 349 to 351
The maximum size allowed (\(maximumSizeAllowed)) is more than the maximum \
amount of bytes that can be written to ByteBuffer \
(\(ByteCount.byteBufferCapacity)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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().

Comment on lines 325 to 327
/// 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`.
Copy link
Contributor

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

Copy link
Contributor

@glbrntt glbrntt left a 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!

@glbrntt glbrntt added the semver/patch No public API change. label Oct 10, 2024
@glbrntt glbrntt changed the title [NIOFileSystem] Avoid crash when reading more than ByteBuffer capacity Throw error when the max read amount is greater than ByteBuffer can tolerate Oct 10, 2024
@glbrntt glbrntt enabled auto-merge (squash) October 10, 2024 09:28
@glbrntt glbrntt merged commit 611fa09 into apple:main Oct 10, 2024
28 of 30 checks passed
@clintonpi clintonpi deleted the avoid-file-system-crash-when-reading-more-than-byte-buffer-capacity branch October 10, 2024 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants