mirror of
https://github.com/apple/swift-nio
synced 2026-11-26 04:57:49 +00:00
Forbid oversized writev calls
This commit is contained in:
1 parent
13d80285c8
commit
be81dbacff
3 files changed
+48
No files matched your search
@@ -33,6 +33,9 @@ private extension IOData {
|
||||
}
|
||||
private typealias PendingWrite = (data: IOData, promise: Promise<Void>?)
|
||||
|
||||
// We need to know the maximum value of a 32-bit int, but in Swift's regular int size. Store it here.
|
||||
private let maxInt = Int(INT32_MAX)
|
||||
|
||||
private func doPendingWriteVectorOperation(pending: MarkedCircularBuffer<PendingWrite>,
|
||||
count: Int,
|
||||
iovecs: UnsafeMutableBufferPointer<IOVector>,
|
||||
@@ -46,10 +49,19 @@ private func doPendingWriteVectorOperation(pending: MarkedCircularBuffer<Pending
|
||||
|
||||
// the numbers of storage refs that we need to decrease later.
|
||||
var c = 0
|
||||
|
||||
// Must not write more than INT32_MAX in one go.
|
||||
var toWrite = 0
|
||||
|
||||
loop: for i in 0..<count {
|
||||
let p = pending[i]
|
||||
switch p.data {
|
||||
case .byteBuffer(let buffer):
|
||||
guard maxInt - toWrite >= buffer.readableBytes else {
|
||||
break loop
|
||||
}
|
||||
toWrite += buffer.readableBytes
|
||||
|
||||
buffer.withUnsafeReadableBytesWithStorageManagement { ptr, storageRef in
|
||||
storageRefs[i] = storageRef.retain()
|
||||
iovecs[i] = iovec(iov_base: UnsafeMutableRawPointer(mutating: ptr.baseAddress!), iov_len: ptr.count)
|
||||
|
||||
@@ -28,6 +28,7 @@ extension ChannelTests {
|
||||
return [
|
||||
("testBasicLifecycle", testBasicLifecycle),
|
||||
("testManyManyWrites", testManyManyWrites),
|
||||
("testWritevLotsOfData", testWritevLotsOfData),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,4 +128,39 @@ public class ChannelTests: XCTestCase {
|
||||
// Start shutting stuff down.
|
||||
try clientChannel.close().wait()
|
||||
}
|
||||
|
||||
func testWritevLotsOfData() throws {
|
||||
let group = try MultiThreadedEventLoopGroup(numThreads: 1)
|
||||
defer {
|
||||
try! group.syncShutdownGracefully()
|
||||
}
|
||||
|
||||
let serverChannel = try ServerBootstrap(group: group)
|
||||
.option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
|
||||
.bind(to: "127.0.0.1", on: 0).wait()
|
||||
|
||||
let clientChannel = try ClientBootstrap(group: group)
|
||||
.connect(to: serverChannel.localAddress!).wait()
|
||||
|
||||
let bufferSize = 1024 * 1024 * 2
|
||||
var buffer = clientChannel.allocator.buffer(capacity: bufferSize)
|
||||
for _ in 0..<bufferSize {
|
||||
buffer.write(staticString: "a")
|
||||
}
|
||||
|
||||
var written = 0
|
||||
while written <= Int(INT32_MAX) {
|
||||
clientChannel.write(data: NIOAny(buffer), promise: nil)
|
||||
written += bufferSize
|
||||
}
|
||||
|
||||
do {
|
||||
_ = try clientChannel.flush().wait()
|
||||
} catch let error {
|
||||
XCTFail("Error occured: \(error)")
|
||||
}
|
||||
|
||||
// Start shutting stuff down.
|
||||
try clientChannel.close().wait()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user