Skip to content

Commit 51c3139

Browse files
authored
Platform: avoid warning from use of _strerror_s
The buffer size for `_strerror` and `_wcserror` are fixed at 94 characters. Use this to allocate the fixed size buffer and build the error message using `_wcserror_s` to avoid the warning as the allocation should be small enough to get stack promoted. Add a workaround for the 5.6.3 release where the Debug Info Verifier will abort due to invalid debug info generation by bumping the minimum to 5.7.
1 parent f01b24d commit 51c3139

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Sources/SwiftWin32/Platform/Error.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,22 @@ extension Error: CustomStringConvertible {
4545
case .errno(let errno):
4646
short = "errno \(errno)"
4747

48-
// Short-circuit the formatting path as this does not do a `LocalAlloc`
49-
// and does not use `FormatMessageW`.
50-
guard let description = _wcserror(errno) else {
51-
return short
48+
// MSDN indicates that the returned string can have a maximum of 94
49+
// characters, so allocate 95 characters.
50+
#if swift(>=5.7)
51+
return withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: 95) {
52+
let result: errno_t = _wcserror_s($0.baseAddress, $0.count, errno)
53+
guard result == 0 else { return short }
54+
return "\(short) - \(String(decodingCString: $0.baseAddress!, as: UTF16.self))"
5255
}
53-
return "\(short) - \(String(decodingCString: description, as: UTF16.self))"
56+
#else
57+
let buffer: UnsafeMutablePointer<wchar_t> = .allocate(capacity: 95)
58+
defer { buffer.deallocate() }
59+
60+
let result: errno_t = _wcserror_s(buffer, 95, errno)
61+
guard result == 0 else { return short }
62+
return "\(short) - \(String(decodingCString: buffer, as: UTF16.self))"
63+
#endif
5464

5565
case .win32(let error):
5666
short = "Win32 Error \(error)"

0 commit comments

Comments
 (0)