From 74f4f1c0b140a73d94cbce95ec437e0208b6540d Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Wed, 3 Sep 2025 23:14:55 -0700 Subject: [PATCH] Cleanup socketport construction (#668) --- .../SecretAgentKit/SocketController.swift | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Sources/Packages/Sources/SecretAgentKit/SocketController.swift b/Sources/Packages/Sources/SecretAgentKit/SocketController.swift index 19b040a..892324f 100644 --- a/Sources/Packages/Sources/SecretAgentKit/SocketController.swift +++ b/Sources/Packages/Sources/SecretAgentKit/SocketController.swift @@ -133,20 +133,22 @@ private extension SocketPort { convenience init(path: String) { var addr = sockaddr_un() - addr.sun_family = sa_family_t(AF_UNIX) - var len: Int = 0 - withUnsafeMutablePointer(to: &addr.sun_path.0) { pointer in + let length = withUnsafeMutablePointer(to: &addr.sun_path.0) { pointer in path.withCString { cstring in - len = strlen(cstring) + let len = strlen(cstring) strncpy(pointer, cstring, len) + return len } } - addr.sun_len = UInt8(len+2) + // This doesn't seem to be _strictly_ neccessary with SocketPort. + // but just for good form. + addr.sun_family = sa_family_t(AF_UNIX) + // This mirrors the SUN_LEN macro format. + addr.sun_len = UInt8(MemoryLayout.size - MemoryLayout.size(ofValue: addr.sun_path) + length) - var data: Data! - withUnsafePointer(to: &addr) { pointer in - data = Data(bytes: pointer, count: MemoryLayout.size) + let data = withUnsafePointer(to: &addr) { pointer in + Data(bytes: pointer, count: MemoryLayout.size) } self.init(protocolFamily: AF_UNIX, socketType: SOCK_STREAM, protocol: 0, address: data)!