diff --git a/Sources/Subprocess/Platforms/Subprocess+Windows.swift b/Sources/Subprocess/Platforms/Subprocess+Windows.swift index e511652a..9668ccaa 100644 --- a/Sources/Subprocess/Platforms/Subprocess+Windows.swift +++ b/Sources/Subprocess/Platforms/Subprocess+Windows.swift @@ -1691,36 +1691,42 @@ extension UInt8 { internal func fillNullTerminatedWideStringBuffer( initialSize: DWORD, maxSize: DWORD, - _ body: (UnsafeMutableBufferPointer) throws(SubprocessError.WindowsError) -> DWORD + _ body: (UnsafeMutableBufferPointer) -> DWORD ) throws(SubprocessError.WindowsError) -> String { + enum FillResult { + case filled(String) + case insufficient + case failed(DWORD) + } var bufferCount = max(1, min(initialSize, maxSize)) while bufferCount <= maxSize { - do { - if let result = try withUnsafeTemporaryAllocation( - of: WCHAR.self, capacity: Int(bufferCount), - { buffer throws(SubprocessError.WindowsError) in - let count = try body(buffer) - switch count { - case 0: - throw SubprocessError.WindowsError(win32Error: GetLastError()) - case 1..=6.3) - throw error - #else - throw error as! SubprocessError.WindowsError - #endif + } + switch result { + case .filled(let string): + return string + case .failed(let win32Error): + throw SubprocessError.WindowsError(win32Error: win32Error) + case .insufficient: + bufferCount *= 2 } } throw SubprocessError.WindowsError(win32Error: DWORD(ERROR_INSUFFICIENT_BUFFER))