Skip to content

Commit b62d6aa

Browse files
committed
improve deinit for the dead name case
1 parent 4038189 commit b62d6aa

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Sources/System/MachPort.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ public enum Mach {
8383
deinit {
8484
if _name == 0xFFFFFFFF /* MACH_PORT_DEAD */ {
8585
precondition(RightType.self != ReceiveRight.self, "Receive rights cannot be dead names")
86-
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_DEAD_NAME, -1))
86+
if RightType.self == SendRight.self {
87+
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND, -1))
88+
} else if RightType.self == SendOnceRight.self {
89+
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND_ONCE, -1))
90+
}
8791
} else {
8892
if RightType.self == ReceiveRight.self {
8993
_machPrecondition(mach_port_destruct(mach_task_self_, _name, 0, _context))
@@ -135,6 +139,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
135139
/// The underlying port right will be automatically deallocated when
136140
/// the Mach.Port object is destroyed.
137141
public init(name: mach_port_name_t, context: mach_port_context_t) {
142+
precondition(name != mach_port_name_t(MACH_PORT_NULL), "Mach.Port cannot be initialized with MACH_PORT_NULL")
138143
self._name = name
139144
self._context = context
140145
}

Tests/SystemTests/MachPortTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@ final class MachPortTests: XCTestCase {
210210
XCTAssertEqual(context, $1)
211211
}
212212
}
213+
214+
func testDeinitDeadSendRight() throws {
215+
let send = Mach.Port<Mach.SendRight>(name: 0xffffffff)
216+
send.withBorrowedName {
217+
XCTAssertEqual($0, .max)
218+
}
219+
_ = consume send
220+
221+
let send1 = Mach.Port<Mach.SendOnceRight>(name: 0xffffffff)
222+
send1.withBorrowedName {
223+
XCTAssertEqual($0, .max)
224+
}
225+
_ = consume send1
226+
}
213227
}
214228

215229
#endif

0 commit comments

Comments
 (0)