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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Sources/NIOFileSystem/ByteCount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ public struct ByteCount: Hashable, Sendable {
}
}

extension 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)
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 { ... }?


/// A ``ByteCount`` for the maximum amount of bytes that can be written to `ByteBuffer`.
internal static var byteBufferCapacity: ByteCount {
ByteCount(bytes: Int64(byteBufferMaxIndex))
}
}

extension ByteCount: AdditiveArithmetic {
public static var zero: ByteCount { ByteCount(bytes: 0) }

Expand Down
17 changes: 16 additions & 1 deletion Sources/NIOFileSystem/FileHandleProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ extension ReadableFileHandleProtocol {
///
/// - Important: This method checks whether the file is seekable or not (i.e., whether it's a socket,
/// pipe or FIFO), and will throw ``FileSystemError/Code-swift.struct/unsupported`` if
/// an offset other than zero is passed.
/// 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

///
/// - Parameters:
/// - offset: The absolute offset into the file to read from. Defaults to zero.
Expand All @@ -340,6 +342,19 @@ extension ReadableFileHandleProtocol {
let fileSize = Int64(info.size)
let readSize = max(Int(fileSize - offset), 0)

if maximumSizeAllowed > .byteBufferCapacity {
throw FileSystemError(
code: .resourceExhausted,
message: """
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().

""",
cause: nil,
location: .here()
)
}

if readSize > maximumSizeAllowed.bytes {
throw FileSystemError(
code: .resourceExhausted,
Expand Down
15 changes: 15 additions & 0 deletions Tests/NIOFileSystemIntegrationTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading