Skip to content

Commit 0186a12

Browse files
authored
Merge pull request #2204 from apple/getting-around-to-sweeping
Foundation: avoid unsafe allocations
2 parents 2e6971e + 157567c commit 0186a12

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

Foundation/FileManager+Win32.swift

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,33 @@ extension FileManager {
2727
internal func _mountedVolumeURLs(includingResourceValuesForKeys propertyKeys: [URLResourceKey]?, options: VolumeEnumerationOptions = []) -> [URL]? {
2828
var urls: [URL] = []
2929

30-
var wszVolumeName: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(MAX_PATH))
31-
defer { wszVolumeName.deallocate() }
30+
var wszVolumeName: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(MAX_PATH))
3231

33-
var hVolumes: HANDLE = FindFirstVolumeW(wszVolumeName.baseAddress, DWORD(wszVolumeName.count))
32+
var hVolumes: HANDLE = FindFirstVolumeW(&wszVolumeName, DWORD(wszVolumeName.count))
3433
guard hVolumes != INVALID_HANDLE_VALUE else { return nil }
3534
defer { FindVolumeClose(hVolumes) }
3635

3736
repeat {
3837
var dwCChReturnLength: DWORD = 0
39-
GetVolumePathNamesForVolumeNameW(wszVolumeName.baseAddress, nil, 0, &dwCChReturnLength)
38+
GetVolumePathNamesForVolumeNameW(&wszVolumeName, nil, 0, &dwCChReturnLength)
4039

41-
var wszPathNames: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(dwCChReturnLength + 1))
42-
defer { wszPathNames.deallocate() }
43-
44-
if !GetVolumePathNamesForVolumeNameW(wszVolumeName.baseAddress, wszPathNames.baseAddress, DWORD(wszPathNames.count), &dwCChReturnLength) {
40+
var wszPathNames: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(dwCChReturnLength + 1))
41+
if !GetVolumePathNamesForVolumeNameW(&wszVolumeName, &wszPathNames, DWORD(wszPathNames.count), &dwCChReturnLength) {
4542
// TODO(compnerd) handle error
4643
continue
4744
}
4845

4946
var pPath: DWORD = 0
5047
repeat {
51-
let path: String = String(decodingCString: wszPathNames.baseAddress! + Int(pPath), as: UTF16.self)
48+
let path: String = String(decodingCString: &wszPathNames[Int(pPath)], as: UTF16.self)
5249
if path.length == 0 {
5350
break
5451
}
5552
urls.append(URL(fileURLWithPath: path, isDirectory: true))
5653
pPath += DWORD(path.length + 1)
5754
} while pPath < dwCChReturnLength
58-
} while FindNextVolumeW(hVolumes, wszVolumeName.baseAddress, DWORD(wszVolumeName.count))
55+
} while FindNextVolumeW(hVolumes, &wszVolumeName, DWORD(wszVolumeName.count))
56+
5957
return urls
6058
}
6159
internal func _urls(for directory: SearchPathDirectory, in domainMask: SearchPathDomainMask) -> [URL] {
@@ -245,17 +243,16 @@ extension FileManager {
245243

246244
try path.withCString(encodedAs: UTF16.self) {
247245
let dwLength: DWORD = GetFullPathNameW($0, 0, nil, nil)
248-
let szVolumePath: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(dwLength + 1))
249-
defer { szVolumePath.deallocate() }
246+
var szVolumePath: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(dwLength + 1))
250247

251-
guard GetVolumePathNameW($0, szVolumePath.baseAddress, dwLength) else {
248+
guard GetVolumePathNameW($0, &szVolumePath, dwLength) else {
252249
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
253250
}
254251

255252
var liTotal: ULARGE_INTEGER = ULARGE_INTEGER()
256253
var liFree: ULARGE_INTEGER = ULARGE_INTEGER()
257254

258-
guard GetDiskFreeSpaceExW(szVolumePath.baseAddress, nil, &liTotal, &liFree) else {
255+
guard GetDiskFreeSpaceExW(&szVolumePath, nil, &liTotal, &liFree) else {
259256
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
260257
}
261258

@@ -297,11 +294,10 @@ extension FileManager {
297294
}
298295

299296
let dwLength: DWORD = GetFinalPathNameByHandleW(hFile, nil, 0, DWORD(FILE_NAME_NORMALIZED))
300-
let szPath: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(dwLength + 1))
301-
defer { szPath.deallocate() }
297+
var szPath: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(dwLength + 1))
302298

303-
GetFinalPathNameByHandleW(hFile, szPath.baseAddress, dwLength, DWORD(FILE_NAME_NORMALIZED))
304-
return String(decodingCString: szPath.baseAddress!, as: UTF16.self)
299+
GetFinalPathNameByHandleW(hFile, &szPath, dwLength, DWORD(FILE_NAME_NORMALIZED))
300+
return String(decodingCString: &szPath, as: UTF16.self)
305301
}
306302

307303
internal func _copyRegularFile(atPath srcPath: String, toPath dstPath: String, variant: String = "Copy") throws {
@@ -469,11 +465,10 @@ extension FileManager {
469465

470466
internal func _currentDirectoryPath() -> String {
471467
let dwLength: DWORD = GetCurrentDirectoryW(0, nil)
472-
var szDirectory: UnsafeMutableBufferPointer<WCHAR> = UnsafeMutableBufferPointer.allocate(capacity: Int(dwLength + 1))
473-
defer { szDirectory.deallocate() }
468+
var szDirectory: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(dwLength + 1))
474469

475-
GetCurrentDirectoryW(dwLength, szDirectory.baseAddress)
476-
return String(decodingCString: szDirectory.baseAddress!, as: UTF16.self)
470+
GetCurrentDirectoryW(dwLength, &szDirectory)
471+
return String(decodingCString: &szDirectory, as: UTF16.self)
477472
}
478473

479474
@discardableResult

Foundation/Host.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ open class Host: NSObject {
105105

106106
ulResult = GetAdaptersAddresses(ULONG(AF_UNSPEC), 0, nil, arAdapterInfo, &ulSize)
107107

108-
var buffer: UnsafeMutablePointer<WCHAR> =
109-
UnsafeMutablePointer<WCHAR>.allocate(capacity: Int(NI_MAXHOST))
110-
defer { buffer.deallocate() }
108+
var buffer: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(NI_MAXHOST))
111109

112110
var arCurrentAdapterInfo: UnsafeMutablePointer<IP_ADAPTER_ADDRESSES>? =
113111
arAdapterInfo
@@ -120,9 +118,9 @@ open class Host: NSObject {
120118
case ADDRESS_FAMILY(AF_INET), ADDRESS_FAMILY(AF_INET6):
121119
if GetNameInfoW(arCurrentAddress.Address.lpSockaddr,
122120
arCurrentAddress.Address.iSockaddrLength,
123-
buffer, DWORD(NI_MAXHOST),
121+
&buffer, DWORD(NI_MAXHOST),
124122
nil, 0, NI_NUMERICHOST) == 0 {
125-
_addresses.append(String(decodingCString: buffer,
123+
_addresses.append(String(decodingCString: &buffer,
126124
as: UTF16.self))
127125
}
128126
default: break
@@ -190,9 +188,7 @@ open class Host: NSObject {
190188
guard bSucceeded == true else { return }
191189
defer { FreeAddrInfoW(aiResult) }
192190

193-
let wszHostName =
194-
UnsafeMutablePointer<WCHAR>.allocate(capacity: Int(NI_MAXHOST))
195-
defer { wszHostName.deallocate() }
191+
var wszHostName: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(NI_MAXHOST))
196192

197193
while aiResult != nil {
198194
let aiInfo: ADDRINFOW = aiResult!.pointee
@@ -209,9 +205,9 @@ open class Host: NSObject {
209205
}
210206

211207
let lookup = { (content: inout [String], flags: Int32) in
212-
if GetNameInfoW(aiInfo.ai_addr, sa_len, wszHostName,
208+
if GetNameInfoW(aiInfo.ai_addr, sa_len, &wszHostName,
213209
DWORD(NI_MAXHOST), nil, 0, flags) == 0 {
214-
content.append(String(decodingCString: wszHostName,
210+
content.append(String(decodingCString: &wszHostName,
215211
as: UTF16.self))
216212
}
217213
}

0 commit comments

Comments
 (0)