Skip to content

Commit fd147c8

Browse files
authored
SwiftWin32: compensate for GetComputerNameW's disregard of the null terminator
### Issue `GetComputerNameW` disregards the null terminator when returning the size of the buffer. This leads to the following error when using `Device.name`: `Swift/CString.swift:284: Fatal error: input of decodeCString(_:as:repairingInvalidCodeUnits:) must be null-terminated` ### Solution Compensated the buffer size to account for the null terminator.
1 parent fe0b6b8 commit fd147c8

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Sources/SwiftWin32/App and Environment/Device.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct Device {
1515
let value: [WCHAR] =
1616
Array<WCHAR>(unsafeUninitializedCapacity: Int(MAX_COMPUTERNAME_LENGTH) + 1) {
1717
var nSize: DWORD = DWORD($0.count)
18-
$1 = GetComputerNameW($0.baseAddress!, &nSize) ? Int(nSize) : 0
18+
$1 = GetComputerNameW($0.baseAddress!, &nSize) ? Int(nSize) + 1 : 0
1919
}
2020
return String(decodingCString: value, as: UTF16.self)
2121
}

0 commit comments

Comments
 (0)