From: Andrej Mihajlov Date: Thu, 22 Sep 2022 08:21:29 +0000 (+0200) Subject: Correct capacity argument when casting memory to sockaddr_* type. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fam%2Ffix-addrinfo-crash;p=thirdparty%2Fwireguard-apple.git Correct capacity argument when casting memory to sockaddr_* type. The capacity argument indicates the number of instances of sockaddr_* and not number of bytes within the pointer. Signed-off-by: Andrej Mihajlov --- diff --git a/Sources/WireGuardKit/IPAddress+AddrInfo.swift b/Sources/WireGuardKit/IPAddress+AddrInfo.swift index 4ade523..253038c 100644 --- a/Sources/WireGuardKit/IPAddress+AddrInfo.swift +++ b/Sources/WireGuardKit/IPAddress+AddrInfo.swift @@ -8,7 +8,10 @@ extension IPv4Address { init?(addrInfo: addrinfo) { guard addrInfo.ai_family == AF_INET else { return nil } - let addressData = addrInfo.ai_addr.withMemoryRebound(to: sockaddr_in.self, capacity: MemoryLayout.size) { ptr -> Data in + let addressData = addrInfo.ai_addr.withMemoryRebound( + to: sockaddr_in.self, + capacity: 1 + ) { ptr -> Data in return Data(bytes: &ptr.pointee.sin_addr, count: MemoryLayout.size) } @@ -20,7 +23,10 @@ extension IPv6Address { init?(addrInfo: addrinfo) { guard addrInfo.ai_family == AF_INET6 else { return nil } - let addressData = addrInfo.ai_addr.withMemoryRebound(to: sockaddr_in6.self, capacity: MemoryLayout.size) { ptr -> Data in + let addressData = addrInfo.ai_addr.withMemoryRebound( + to: sockaddr_in6.self, + capacity: 1 + ) { ptr -> Data in return Data(bytes: &ptr.pointee.sin6_addr, count: MemoryLayout.size) }