|
21 | 21 | #include <lib/core/ErrorStr.h> |
22 | 22 |
|
23 | 23 | #include <string.h> |
24 | | -#include <mutex> |
25 | | -#define CHIP_HAVE_STD_MUTEX 1 |
26 | 24 |
|
27 | 25 | namespace chip { |
28 | 26 | namespace Platform { |
@@ -55,44 +53,17 @@ const char * DescribePlatformError(CHIP_ERROR aError) |
55 | 53 | #else |
56 | 54 | static char errBuf[128]; |
57 | 55 | #endif // CHIP_SYSTEM_CONFIG_THREAD_LOCAL_STORAGE |
58 | | - // Try the POSIX/XSI strerror_r variant (int strerror_r(int, char*, size_t)). |
59 | | -#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L && !defined(_GNU_SOURCE) |
60 | | - if (strerror_r(lError, errBuf, sizeof(errBuf)) == 0) |
| 56 | + // Use strerror() and copy into a thread-local buffer. We intentionally do |
| 57 | + // not attempt to use strerror_r here to avoid cross-toolchain incompatibilities |
| 58 | + // between the POSIX (int) and GNU (char*) variants. |
| 59 | + |
| 60 | + const char * s = strerror(lError); |
| 61 | + if (s != nullptr) |
61 | 62 | { |
| 63 | + strncpy(errBuf, s, sizeof(errBuf) - 1); |
| 64 | + errBuf[sizeof(errBuf) - 1] = '\0'; |
62 | 65 | return errBuf; |
63 | 66 | } |
64 | | -#elif defined(_GNU_SOURCE) || defined(__GLIBC__) |
65 | | - { |
66 | | - // GNU variant: char *strerror_r(int, char*, size_t) |
67 | | - char * s = strerror_r(lError, errBuf, sizeof(errBuf)); |
68 | | - if (s != nullptr) |
69 | | - { |
70 | | - if (s != errBuf) |
71 | | - { |
72 | | - strncpy(errBuf, s, sizeof(errBuf) - 1); |
73 | | - errBuf[sizeof(errBuf) - 1] = '\0'; |
74 | | - } |
75 | | - return errBuf; |
76 | | - } |
77 | | - } |
78 | | -#else |
79 | | - // No strerror_r declared: fall back to strerror(). To make this safe in |
80 | | - // multi-threaded builds, serialize access to strerror() if std::mutex is |
81 | | - // available; otherwise do an unprotected copy (best-effort). |
82 | | -# ifdef CHIP_HAVE_STD_MUTEX |
83 | | - static std::mutex s_strerror_mutex; |
84 | | - std::lock_guard<std::mutex> lock(s_strerror_mutex); |
85 | | -# endif |
86 | | - { |
87 | | - const char * s = strerror(lError); |
88 | | - if (s != nullptr) |
89 | | - { |
90 | | - strncpy(errBuf, s, sizeof(errBuf) - 1); |
91 | | - errBuf[sizeof(errBuf) - 1] = '\0'; |
92 | | - return errBuf; |
93 | | - } |
94 | | - } |
95 | | -#endif |
96 | 67 |
|
97 | 68 | return "Unknown platform error"; |
98 | 69 | } |
|
0 commit comments