Skip to content

Commit ac850f9

Browse files
authored
Merge pull request swiftlang#1879 from compnerd/compile-fixes
2 parents bcc12c8 + 9e27d7c commit ac850f9

File tree

3 files changed

+59
-41
lines changed

3 files changed

+59
-41
lines changed

Diff for: Foundation/Host.swift

+56-39
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ open class Host: NSObject {
3838
GetComputerNameExA(ComputerNameDnsHostname, nil, &dwLength)
3939
guard dwLength > 0 else { return "localhost" }
4040

41-
let hostname: UnsafeMutablePointer<Int8> =
42-
UnsafeMutableBufferPointer<Int8>.allocate(capacity: Int(dwLength + 1)).baseAddress
41+
guard let hostname: UnsafeMutablePointer<Int8> =
42+
UnsafeMutableBufferPointer<Int8>
43+
.allocate(capacity: Int(dwLength + 1))
44+
.baseAddress else {
45+
return "localhost"
46+
}
4347
defer { hostname.deallocate() }
44-
guard GetComputerNameExA(ComputerNameDnsHostname, hostname, &dwLength) else {
48+
guard GetComputerNameExA(ComputerNameDnsHostname, hostname, &dwLength) != FALSE else {
4549
return "localhost"
4650
}
4751
return String(cString: hostname)
@@ -78,36 +82,42 @@ open class Host: NSObject {
7882
#if os(Android)
7983
return
8084
#elseif os(Windows)
81-
var ulResult: ULONG = 0
82-
8385
var ulSize: ULONG = 0
84-
var arAdapterInfo: UnsafeMutablePointer<IP_ADAPTER_ADDRESSES>?
85-
86-
var buffer: UnsafeMutablePointer<Int8> =
87-
UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
88-
defer { buffer.deallocate() }
86+
var ulResult: ULONG =
87+
GetAdaptersAddresses(ULONG(AF_UNSPEC), 0, nil, nil, &ulSize)
8988

90-
ulResult = GetAdapterAddresses(AF_UNSPEC, 0, nil, nil, &ulSize)
91-
arAdapterInfo = UnsafeMutablePointer<IP_ADAPTER_ADDRESSES>
92-
.allocate(capacity: ulSize / MemoryLayout<IP_ADAPTER_ADDRESSES>.size)
89+
let arAdapterInfo: UnsafeMutablePointer<IP_ADAPTER_ADDRESSES> =
90+
UnsafeMutablePointer<IP_ADAPTER_ADDRESSES_LH>
91+
.allocate(capacity: Int(ulSize) / MemoryLayout<IP_ADAPTER_ADDRESSES>.size)
9392
defer { arAdapterInfo.deallocate() }
9493

95-
ulResult = GetAdapterAddresses(AF_UNSPEC, 0, nil, &arAdapterInfo, &ulSize)
96-
while arAdapterInfo != nil {
97-
var address = arAdapterInfo.FirstUnicastAddress
98-
while address != nil {
99-
switch address.lpSockaddr.sa_family {
100-
case AF_INET, AF_INET6:
101-
if GetNameInfoW(address.lpSockaddr, address.iSockaddrLength,
102-
buffer, socklen_t(NI_MAXHOST), nil, 0,
103-
NI_NUMERICHOST) == 0 {
104-
_addresses.append(String(cString: buffer))
94+
ulResult = GetAdaptersAddresses(ULONG(AF_UNSPEC), 0, nil, arAdapterInfo, &ulSize)
95+
96+
var buffer: UnsafeMutablePointer<WCHAR> =
97+
UnsafeMutablePointer<WCHAR>.allocate(capacity: Int(NI_MAXHOST))
98+
defer { buffer.deallocate() }
99+
100+
var arCurrentAdapterInfo: UnsafeMutablePointer<IP_ADAPTER_ADDRESSES>? =
101+
arAdapterInfo
102+
while arCurrentAdapterInfo != nil {
103+
var arAddress: UnsafeMutablePointer<IP_ADAPTER_UNICAST_ADDRESS>? =
104+
arCurrentAdapterInfo!.pointee.FirstUnicastAddress
105+
while arAddress != nil {
106+
let arCurrentAddress: IP_ADAPTER_UNICAST_ADDRESS = arAddress!.pointee
107+
switch arCurrentAddress.Address.lpSockaddr.pointee.sa_family {
108+
case ADDRESS_FAMILY(AF_INET), ADDRESS_FAMILY(AF_INET6):
109+
if GetNameInfoW(arCurrentAddress.Address.lpSockaddr,
110+
arCurrentAddress.Address.iSockaddrLength,
111+
buffer, DWORD(NI_MAXHOST),
112+
nil, 0, NI_NUMERICHOST) == 0 {
113+
_addresses.append(String(decodingCString: buffer,
114+
as: UTF16.self))
105115
}
106116
default: break
107117
}
108-
address = address.Next
118+
arAddress = arCurrentAddress.Next
109119
}
110-
arAdapterInfo = arAdapterInfo.Next
120+
arCurrentAdapterInfo = arCurrentAdapterInfo!.pointee.Next
111121
}
112122
_resolved = true
113123
#else
@@ -146,7 +156,7 @@ open class Host: NSObject {
146156
if _type == .current { return _resolveCurrent() }
147157

148158
var hints: ADDRINFOW = ADDRINFOW()
149-
ZeroMemory(&hints, MemoryLayout<ADDRINFOW>.size)
159+
memset(&hints, 0, MemoryLayout<ADDRINFOW>.size)
150160

151161
switch (_type) {
152162
case .name:
@@ -158,34 +168,41 @@ open class Host: NSObject {
158168
}
159169
hints.ai_family = AF_UNSPEC
160170
hints.ai_socktype = SOCK_STREAM
161-
hints.ai_protocol = IPPROTO_TCP
171+
hints.ai_protocol = IPPROTO_TCP.rawValue
162172

163173
var aiResult: UnsafeMutablePointer<ADDRINFOW>?
164-
if GetAddrInfoW(info, nil, &hints, &aiResult) != 0 { return }
174+
var bSucceeded: Bool = false
175+
info.withCString(encodedAs: UTF16.self) {
176+
if GetAddrInfoW($0, nil, &hints, &aiResult) == 0 {
177+
bSucceeded = true
178+
}
179+
}
180+
guard bSucceeded == true else { return }
165181
defer { FreeAddrInfoW(aiResult) }
166182

167-
let szHostName =
168-
UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
169-
defer { szHostName.deallocate() }
183+
let wszHostName =
184+
UnsafeMutablePointer<WCHAR>.allocate(capacity: Int(NI_MAXHOST))
185+
defer { wszHostName.deallocate() }
170186

171187
while aiResult != nil {
172-
let aiInfo: ADDRINFOW = aiResult.pointee
173-
let sa_len: socklen_t = 0
188+
let aiInfo: ADDRINFOW = aiResult!.pointee
189+
var sa_len: socklen_t = 0
174190

175191
switch aiInfo.ai_family {
176192
case AF_INET:
177-
sa_len = MemoryLayout<sockaddr_in>.size
193+
sa_len = socklen_t(MemoryLayout<sockaddr_in>.size)
178194
case AF_INET6:
179-
sa_len = MemoryLayout<sockaddr_in6>.size
195+
sa_len = socklen_t(MemoryLayout<sockaddr_in6>.size)
180196
default:
181-
result = aiInfo.ai_next
197+
aiResult = aiInfo.ai_next
182198
continue
183199
}
184200

185201
let lookup = { (content: inout [String], flags: Int32) in
186-
if GetNameInfoW(aiInfo.ai_addr, sa_len, szHostName,
187-
socklen_t(NI_MAXHOST), nil, 0, flags) == 0 {
188-
content.append(String(cString: szHostName))
202+
if GetNameInfoW(aiInfo.ai_addr, sa_len, wszHostName,
203+
DWORD(NI_MAXHOST), nil, 0, flags) == 0 {
204+
content.append(String(decodingCString: wszHostName,
205+
as: UTF16.self))
189206
}
190207
}
191208

Diff for: Foundation/NSPlatform.swift

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fileprivate let _NSPageSize = Int(vm_page_size)
1212
#elseif os(Linux) || os(Android)
1313
fileprivate let _NSPageSize = Int(getpagesize())
1414
#elseif os(Windows)
15+
import WinSDK
1516
fileprivate var _NSPageSize: Int {
1617
var siInfo: SYSTEM_INFO = SYSTEM_INFO()
1718
GetSystemInfo(&siInfo)

Diff for: Foundation/Thread.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ open class Thread : NSObject {
189189

190190
#if os(Windows) && !CYGWIN
191191
internal var _attr: _CFThreadAttributes =
192-
_CFThreadAttributes(dwSizeOfAttributes: MemoryLayout<_CFThreadAttributes>.size,
192+
_CFThreadAttributes(dwSizeOfAttributes: DWORD(MemoryLayout<_CFThreadAttributes>.size),
193193
dwThreadStackReservation: 0)
194194
#elseif CYGWIN
195195
internal var _attr : pthread_attr_t? = nil
@@ -264,7 +264,7 @@ open class Thread : NSObject {
264264
return Int(ulLowLimit)
265265
}
266266
set {
267-
_attr.dwThreadStackReservation = newValue
267+
_attr.dwThreadStackReservation = DWORD(newValue)
268268
}
269269
}
270270
#else

0 commit comments

Comments
 (0)