Cleanup socketport construction (#668)

This commit is contained in:
Max Goedjen 2025-09-03 23:14:55 -07:00 committed by GitHub
parent c8cf0db1c5
commit 74f4f1c0b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -133,20 +133,22 @@ private extension SocketPort {
convenience init(path: String) { convenience init(path: String) {
var addr = sockaddr_un() var addr = sockaddr_un()
addr.sun_family = sa_family_t(AF_UNIX)
var len: Int = 0 let length = withUnsafeMutablePointer(to: &addr.sun_path.0) { pointer in
withUnsafeMutablePointer(to: &addr.sun_path.0) { pointer in
path.withCString { cstring in path.withCString { cstring in
len = strlen(cstring) let len = strlen(cstring)
strncpy(pointer, cstring, len) 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<sockaddr_un>.size - MemoryLayout.size(ofValue: addr.sun_path) + length)
var data: Data! let data = withUnsafePointer(to: &addr) { pointer in
withUnsafePointer(to: &addr) { pointer in Data(bytes: pointer, count: MemoryLayout<sockaddr_un>.size)
data = Data(bytes: pointer, count: MemoryLayout<sockaddr_un>.size)
} }
self.init(protocolFamily: AF_UNIX, socketType: SOCK_STREAM, protocol: 0, address: data)! self.init(protocolFamily: AF_UNIX, socketType: SOCK_STREAM, protocol: 0, address: data)!