mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
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.
This commit is contained in:
1 parent
013178c00e
commit
6c5077b2d2
3 files changed
+26
-10
No files matched your search
@@ -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.
|
||||
|
||||
+17
-6
@@ -45,11 +45,7 @@ unsigned long long _system_get_ST_NOSYMFOLLOW(void) { return ST_NOSYMFOLLOW; }
|
||||
|
||||
#include <CSystemShared.h>
|
||||
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user