Skip to content

Commit 64b66f3

Browse files
committed
Use proper API for adding slash rather than just appending it
1 parent fc9bbca commit 64b66f3

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Sources/Testing/ExitTests/SpawnProcess.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func spawnExecutable(
229229
return pid
230230
}
231231
}
232+
232233
#elseif os(Windows)
233234
return try _withStartupInfoEx(attributeCount: 1) { startupInfo in
234235
func inherit(_ fileHandle: borrowing FileHandle) throws -> HANDLE? {
@@ -296,15 +297,17 @@ func spawnExecutable(
296297
// unlikely to be deleted, one hopes).
297298
//
298299
// SEE: https://devblogs.microsoft.com/oldnewthing/20101109-00/?p=12323
299-
let workingDirectoryPath: UnsafeMutablePointer<wchar_t>? = {
300-
var systemDrive = Environment.variable(named: "SYSTEMDRIVE") ?? "C:"
301-
if systemDrive.last == ":" {
302-
systemDrive = #"\#(systemDrive)\"#
303-
}
304-
return systemDrive.withCString(encodedAs: UTF16.self) { _wcsdup($0) }
305-
}()
300+
var workingDirectoryPath = UnsafeMutableBufferPointer<wchar_t>.allocate(capacity: Int(MAX_PATH))
306301
defer {
307-
free(workingDirectoryPath)
302+
workingDirectoryPath.deallocate()
303+
}
304+
let systemDrive = Environment.variable(named: "SYSTEMDRIVE") ?? "C:"
305+
systemDrive.withCString(encodedAs: UTF16.self) { systemDrive in
306+
wcscpy_s(workingDirectoryPath.baseAddress!, workingDirectoryPath.count, systemDrive)
307+
let rAddBackslash = PathCchAddBackslashEx(workingDirectoryPath.baseAddress!, workingDirectoryPath.count, nil, nil)
308+
guard rAddBackslash == S_OK || rAddBackslash == S_FALSE else {
309+
fatalError("Unexpected error when normalizing system drive path '\(systemDrive)': HRESULT(\(rAddBackslash))")
310+
}
308311
}
309312

310313
var flags = DWORD(CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT)

0 commit comments

Comments
 (0)