mirror of
https://github.com/apple/swift-system
synced 2026-11-24 04:38:14 +00:00
strerror(3) isn't guaranteed to be thread-safe: for an unknown error number, it may format the message into a static buffer shared by all threads, so concurrent calls to Errno.description could return each other's text. Add a csystem_strerror_r shim to CSystem that writes into a caller-provided buffer. It hides the difference between the XSI strerror_r (Darwin, BSDs, musl, WASI) and the GNU variant that glibc and bionic provide under _GNU_SOURCE, and uses strerror_s on Windows. Errno.description now formats into a stack buffer through this shim. Also add tests for Errno.description, which were a TODO. Fixes #156
29 lines
1008 B
C
29 lines
1008 B
C
/*
|
|
This source file is part of the Swift System open source project
|
|
|
|
Copyright (c) 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
|
|
*/
|
|
|
|
#if !defined(_WIN32)
|
|
extern int csystem_posix_pipe2(int fildes[2], int flag);
|
|
#endif
|
|
|
|
extern int csystem_posix_dup3(int fildes, int fildes2, int flag);
|
|
|
|
#include <stddef.h>
|
|
|
|
// Thread-safe replacement for strerror(3).
|
|
//
|
|
// 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".
|
|
//
|
|
// This hides the differences between the XSI and GNU variants of strerror_r,
|
|
// and uses strerror_s on Windows.
|
|
extern int csystem_strerror_r(int errnum, char *buf, size_t buflen);
|