swift-nio icon indicating copy to clipboard operation
swift-nio copied to clipboard

NIOFileSystem crashes when loading more than 2GB

Open weissi opened this issue 5 months ago • 1 comments

Expected behavior

When requesting impossible things (e.g. loading more than 2GB into a ByteBuffer), NIOFileSystem should raise an error and not crash.

Actual behavior

Crash

Steps to reproduce

request.body = try await Data(buffer: ByteBuffer(
    contentsOf: "/tmp/4GBFile",
    maximumSizeAllowed: .gibibytes(4)
))

problem

Thread 8 Crashed:: NIO-SGLTN-0-#7
0   0x102fc5490 Swift runtime failure: arithmetic overflow + 0 [inlined]
1   0x102fc5490 ByteBuffer._setBytes(_:at:) + 420 (ByteBuffer-core.swift:491)
2   0x102fc9190 ByteBuffer.setBytes(_:at:) + 88 (ByteBuffer-core.swift:1007)
3   0x102fbd7d8 closure #1 in ByteBuffer.setBuffer(_:at:) + 72 (ByteBuffer-aux.swift:399)
4   0x102fbd810 partial apply for closure #1 in ByteBuffer.setBuffer(_:at:) + 28
5   0x102fc72a0 ByteBuffer.withUnsafeReadableBytes<A>(_:) + 448 (ByteBuffer-core.swift:748)
6   0x102fbd764 ByteBuffer.setBuffer(_:at:) + 196 (ByteBuffer-aux.swift:398)
7   0x102fbd950 ByteBuffer.writeBuffer(_:) + 308 (ByteBuffer-aux.swift:412)
8   0x102fbe534 ByteBuffer.writeImmutableBuffer(_:) + 204 (ByteBuffer-aux.swift:512)
9   0x1038a6400 ReadableFileHandleProtocol.readToEnd(fromAbsoluteOffset:maximumSizeAllowed:) + 364 (FileHandleProtocol.swift:389)
10  0x10387d38d closure #1 in ByteBuffer.init<A>(contentsOf:maximumSizeAllowed:fileSystem:) + 1 (ByteBuffer+FileSystem.swift:32)
11  0x10387d595 partial apply for closure #1 in ByteBuffer.init<A>(contentsOf:maximumSizeAllowed:fileSystem:) + 1
12  0x1038df7e5 closure #1 in FileSystemProtocol.withFileHandle<A>(forReadingAt:options:execute:) + 1 (FileSystemProtocol.swift:329)
13  0x1038df9a1 partial apply for closure #1 in FileSystemProtocol.withFileHandle<A>(forReadingAt:options:execute:) + 1
14  0x103911ef1 withUncancellableTearDown<A>(_:tearDown:) + 1 (Cancellation.swift:40)
15  0x1038df3b5 FileSystemProtocol.withFileHandle<A>(forReadingAt:options:execute:) + 1 (FileSystemProtocol.swift:328)
16  0x10387cfa9 ByteBuffer.init<A>(contentsOf:maximumSizeAllowed:fileSystem:) + 1 (ByteBuffer+FileSystem.swift:31)
17  0x10387d705 ByteBuffer.init(contentsOf:maximumSizeAllowed:) + 1 (ByteBuffer+FileSystem.swift:48)

weissi avatar Sep 10 '24 13:09 weissi