Skip to content

Commit a416756

Browse files
authored
PackagePlugin: silence a deprecation warning on Windows (#7109)
Prefer to use the microsoft recommended function for errno string description conversion. This silences a warning and relies on "safer" alternatives (at least ones which will not use a global shared buffer).
1 parent e38c81d commit a416756

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Sources/PackagePlugin/Plugin.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ internal func close(_ fd: CInt) -> CInt {
2626
internal func fileno(_ fh: UnsafeMutablePointer<FILE>?) -> CInt {
2727
return _fileno(fh)
2828
}
29+
30+
internal func strerror(_ errno: CInt) -> String? {
31+
// MSDN indicates that the returned string can have a maximum of 94
32+
// characters, so allocate 95 characters.
33+
return withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: 95) {
34+
let result = _wcserror_s($0.baseAddress, $0.count, errno)
35+
guard result == 0, let baseAddress = $0.baseAddress else { return nil }
36+
return String(decodingCString: baseAddress, as: UTF16.self)
37+
}
38+
}
2939
#endif
3040

3141
//
@@ -267,8 +277,12 @@ extension Plugin {
267277

268278
// Private function to construct an error message from an `errno` code.
269279
fileprivate static func describe(errno: Int32) -> String {
280+
#if os(Windows)
281+
return strerror(errno) ?? String(errno)
282+
#else
270283
if let cStr = strerror(errno) { return String(cString: cStr) }
271284
return String(describing: errno)
285+
#endif
272286
}
273287
}
274288

0 commit comments

Comments
 (0)