mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
Copy strerror message directly into the String buffer
Use String(unsafeUninitializedCapacity:initializingUTF8With:), through the existing backwards-compatible helper, so the message is written straight into the string's storage instead of a stack buffer that is then copied. Also update the copyright year range of Errno.swift. musl's message for an unknown error number doesn't include the number, so skip that check when building against Musl, as on WASI.
This commit is contained in:
1 parent
6c5077b2d2
commit
1c44ed678c
2 files changed
+17
-13
No files matched your search
+14
-11
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors
|
||||
Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors
|
||||
Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
@@ -1552,17 +1552,20 @@ extension Errno: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
@inline(never)
|
||||
public var description: String {
|
||||
// Error messages are short, so 256 bytes is plenty. If one is longer,
|
||||
// csystem_strerror_r truncates it.
|
||||
withUnsafeTemporaryAllocation(of: CChar.self, capacity: 256) { buffer in
|
||||
let base = buffer.baseAddress!
|
||||
base.initialize(to: 0)
|
||||
// Even when strerror_r fails, e.g. with EINVAL for an unknown error
|
||||
// number, most C libraries still write a message like
|
||||
// "Unknown error 1234", so use whatever was written.
|
||||
_ = system_strerror_r(self.rawValue, base, buffer.count)
|
||||
guard base.pointee != 0 else { return "unknown error" }
|
||||
return String(cString: base)
|
||||
// csystem_strerror_r truncates it. The message is written directly into
|
||||
// the new string's storage.
|
||||
let message = String(_unsafeUninitializedCapacity: 256) { buffer in
|
||||
let capacity = buffer.count
|
||||
buffer.withMemoryRebound(to: CChar.self) { buffer in
|
||||
// Even when strerror_r fails, e.g. with EINVAL for an unknown error
|
||||
// number, most C libraries still write a message like
|
||||
// "Unknown error 1234", so use whatever was written. The shim always
|
||||
// NUL-terminates the buffer.
|
||||
_ = system_strerror_r(self.rawValue, buffer.baseAddress!, capacity)
|
||||
}
|
||||
return UnsafeBufferPointer(buffer).firstIndex(of: 0) ?? 0
|
||||
}
|
||||
return message.isEmpty ? "unknown error" : message
|
||||
}
|
||||
|
||||
/// A textual representation,
|
||||
|
||||
@@ -250,8 +250,9 @@ final class ErrnoTest: XCTestCase {
|
||||
// Unknown error numbers still get a message.
|
||||
let unknown = Errno(rawValue: 98765).description
|
||||
XCTAssertFalse(unknown.isEmpty)
|
||||
#if !os(Windows) && !os(WASI)
|
||||
// Windows' message is just "Unknown error", without the number.
|
||||
#if !os(Windows) && !os(WASI) && !canImport(Musl)
|
||||
// Windows' message is just "Unknown error", and musl's (also used by
|
||||
// WASI) is "No error information", without the number.
|
||||
XCTAssert(unknown.contains("98765"), unknown)
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user