From 1c44ed678c6d5da2880d0179ef9e2b7c0cd0bfb4 Mon Sep 17 00:00:00 2001 From: Lalita Hegade Date: Thu, 12 Nov 2026 08:13:35 +0100 Subject: [PATCH] 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. --- Sources/System/Errno.swift | 25 ++++++++++++++----------- Tests/SystemTests/ErrnoTest.swift | 5 +++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Sources/System/Errno.swift b/Sources/System/Errno.swift index 4eb7bf1..187cf56 100644 --- a/Sources/System/Errno.swift +++ b/Sources/System/Errno.swift @@ -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, diff --git a/Tests/SystemTests/ErrnoTest.swift b/Tests/SystemTests/ErrnoTest.swift index 71cd016..efbef25 100644 --- a/Tests/SystemTests/ErrnoTest.swift +++ b/Tests/SystemTests/ErrnoTest.swift @@ -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 }