mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-26 04:57:51 +00:00
RegionSink took owners unretained, so a conformer passing a box it created inside withStableRegions handed over a dead one: use-after-free on the first zero-copy extraction. Costs one retain/release per vouched region per gather; retain counts in the parse benchmarks are unchanged.
43 lines
1.9 KiB
Swift
43 lines
1.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIO
|
|
import NIOIMAPCore
|
|
|
|
// This file is what lets a caller holding a `ByteBuffer` append it to an `IMAPInputBuffer` without
|
|
// copying it. `NIOIMAPCore` does not know about NIO, so the conformances live here; `NIOIMAP` re-exports
|
|
// `NIOIMAPCore`, so which of the two modules declares them is invisible to anyone importing `NIOIMAP`.
|
|
//
|
|
// They need no `@retroactive`: the protocols are declared in this package, so this is not a conformance
|
|
// two other modules could also add.
|
|
|
|
extension ByteBuffer: ByteRegion {
|
|
/// Calls the given closure with a pointer to the buffer's readable bytes.
|
|
///
|
|
/// This is `withUnsafeReadableBytes(_:)` under the name `ByteRegion` requires, and it is what
|
|
/// lets a `ByteBuffer` be handed to `NIOIMAPCore` without being copied first.
|
|
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
|
|
try withUnsafeReadableBytes(body)
|
|
}
|
|
}
|
|
|
|
// A copy of a `ByteBuffer` keeps its storage alive, and while copies share storage a write through any of
|
|
// them copies it first — so the readable bytes a copy sees neither move nor change. That lets
|
|
// `IMAPInputBuffer` keep a large one and `makeDispatchData()` donate one, rather than copy.
|
|
extension ByteBuffer: StableAddressByteRegion {
|
|
/// The capacity of the storage, which every copy and slice of this buffer keeps alive: a 1 KiB
|
|
/// slice of a 64 KiB read pins 64 KiB.
|
|
public var pinnedByteCount: Int { storageCapacity }
|
|
}
|