From 6c5077b2d2122d43bea526d7f2bc0e3f379622a8 Mon Sep 17 00:00:00 2001 From: Lalita Hegade Date: Wed, 11 Nov 2026 22:12:05 +0100 Subject: [PATCH] Report truncated strerror messages as ERANGE on every platform strerror_s on Windows and bionic's strerror_r truncate without returning an error, so detect a full buffer in the shim instead. Also avoid the deprecated String(cString:) array overload in the test. --- Sources/CSystem/include/CSystemShared.h | 5 +++-- Sources/CSystem/shims.c | 23 +++++++++++++++++------ Tests/SystemTests/ErrnoTest.swift | 8 ++++++-- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Sources/CSystem/include/CSystemShared.h b/Sources/CSystem/include/CSystemShared.h index 9ae7cd5..ec259bf 100644 --- a/Sources/CSystem/include/CSystemShared.h +++ b/Sources/CSystem/include/CSystemShared.h @@ -20,8 +20,9 @@ extern int csystem_posix_dup3(int fildes, int fildes2, int flag); // Writes the message for `errnum` into `buf`, which holds `buflen` bytes, and // always NUL-terminates it if `buflen` is not zero. Returns 0 on success, or an // error number (e.g. EINVAL for an unknown `errnum`, ERANGE if the message was -// truncated). Even on failure, `buf` may contain a message such as -// "Unknown error 1234". +// truncated). A message that exactly fills the buffer is also reported as +// ERANGE, because not every C library can tell that apart from truncation. +// Even on failure, `buf` may contain a message such as "Unknown error 1234". // // This hides the differences between the XSI and GNU variants of strerror_r, // and uses strerror_s on Windows. diff --git a/Sources/CSystem/shims.c b/Sources/CSystem/shims.c index 942b198..9b18aa1 100644 --- a/Sources/CSystem/shims.c +++ b/Sources/CSystem/shims.c @@ -45,11 +45,7 @@ unsigned long long _system_get_ST_NOSYMFOLLOW(void) { return ST_NOSYMFOLLOW; } #include -extern int csystem_strerror_r(int errnum, char *buf, size_t buflen) { - if (buf == NULL || buflen == 0) { - return ERANGE; - } - buf[0] = '\0'; +static int csystem_strerror_r_impl(int errnum, char *buf, size_t buflen) { #if defined(_WIN32) return strerror_s(buf, buflen, errnum); #elif defined(_GNU_SOURCE) && \ @@ -78,11 +74,26 @@ extern int csystem_strerror_r(int errnum, char *buf, size_t buflen) { if (result == -1) { result = errno; } - buf[buflen - 1] = '\0'; return result; #endif } +extern int csystem_strerror_r(int errnum, char *buf, size_t buflen) { + if (buf == NULL || buflen == 0) { + return ERANGE; + } + buf[0] = '\0'; + int result = csystem_strerror_r_impl(errnum, buf, buflen); + buf[buflen - 1] = '\0'; + // strerror_s on Windows and bionic's strerror_r silently truncate the + // message and return 0. Report a message that fills the whole buffer as + // ERANGE on every platform, since it may have been truncated. + if (result == 0 && strlen(buf) == buflen - 1) { + result = ERANGE; + } + return result; +} + #if !defined(_WIN32) && !defined(__wasi__) && !defined(__APPLE__) #define HAVE_PIPE2_DUP3 #endif diff --git a/Tests/SystemTests/ErrnoTest.swift b/Tests/SystemTests/ErrnoTest.swift index 13ebe58..71cd016 100644 --- a/Tests/SystemTests/ErrnoTest.swift +++ b/Tests/SystemTests/ErrnoTest.swift @@ -282,13 +282,17 @@ final class ErrnoTest: XCTestCase { func testStrerrorShim() { var buffer = [CChar](repeating: 1, count: 256) XCTAssertEqual(csystem_strerror_r(ENOENT, &buffer, buffer.count), 0) - XCTAssertEqual(String(cString: buffer), "No such file or directory") + XCTAssertEqual( + buffer.withUnsafeBufferPointer { String(cString: $0.baseAddress!) }, + "No such file or directory") // A buffer that is too small yields a truncated, NUL-terminated message. var small = [CChar](repeating: 1, count: 4) XCTAssertEqual(csystem_strerror_r(ENOENT, &small, small.count), ERANGE) #if !os(Windows) - XCTAssertEqual(String(cString: small), "No ") + XCTAssertEqual( + small.withUnsafeBufferPointer { String(cString: $0.baseAddress!) }, + "No ") #endif XCTAssertEqual(small[3], 0)