Skip to content

Commit ec9b08e

Browse files
committed
Fix build errors
1 parent 95e4dca commit ec9b08e

File tree

3 files changed

+13
-139
lines changed

3 files changed

+13
-139
lines changed

src/platform/PlatformError.cpp

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include <lib/core/ErrorStr.h>
2222

2323
#include <string.h>
24-
#include <mutex>
25-
#define CHIP_HAVE_STD_MUTEX 1
2624

2725
namespace chip {
2826
namespace Platform {
@@ -55,44 +53,17 @@ const char * DescribePlatformError(CHIP_ERROR aError)
5553
#else
5654
static char errBuf[128];
5755
#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)
6162
{
63+
strncpy(errBuf, s, sizeof(errBuf) - 1);
64+
errBuf[sizeof(errBuf) - 1] = '\0';
6265
return errBuf;
6366
}
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
9667

9768
return "Unknown platform error";
9869
}

src/system/StrErrorUtil.h

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/system/SystemError.cpp

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
#include <limits>
4141
#include <stddef.h>
4242
#include <string.h>
43-
#include <mutex>
44-
#define CHIP_HAVE_STD_MUTEX 1
4543

4644
namespace chip {
4745
namespace System {
@@ -92,44 +90,14 @@ DLL_EXPORT const char * DescribeErrorPOSIX(CHIP_ERROR aError)
9290
#else
9391
static char errBuf[128];
9492
#endif // CHIP_SYSTEM_CONFIG_THREAD_LOCAL_STORAGE
95-
// Try the POSIX/XSI strerror_r variant (int strerror_r(int, char*, size_t)).
96-
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L && !defined(_GNU_SOURCE)
97-
if (strerror_r(lError, errBuf, sizeof(errBuf)) == 0)
93+
94+
const char * s = strerror(lError);
95+
if (s != nullptr)
9896
{
97+
strncpy(errBuf, s, sizeof(errBuf) - 1);
98+
errBuf[sizeof(errBuf) - 1] = '\0';
9999
return errBuf;
100100
}
101-
#elif defined(_GNU_SOURCE) || defined(__GLIBC__)
102-
{
103-
// GNU variant: char *strerror_r(int, char*, size_t)
104-
char * s = strerror_r(lError, errBuf, sizeof(errBuf));
105-
if (s != nullptr)
106-
{
107-
if (s != errBuf)
108-
{
109-
strncpy(errBuf, s, sizeof(errBuf) - 1);
110-
errBuf[sizeof(errBuf) - 1] = '\0';
111-
}
112-
return errBuf;
113-
}
114-
}
115-
#else
116-
// No strerror_r declared: fall back to strerror(). To make this safe in
117-
// multi-threaded builds, serialize access to strerror() if std::mutex is
118-
// available; otherwise do an unprotected copy (best-effort).
119-
# ifdef CHIP_HAVE_STD_MUTEX
120-
static std::mutex s_strerror_mutex;
121-
std::lock_guard<std::mutex> lock(s_strerror_mutex);
122-
# endif
123-
{
124-
const char * s = strerror(lError);
125-
if (s != nullptr)
126-
{
127-
strncpy(errBuf, s, sizeof(errBuf) - 1);
128-
errBuf[sizeof(errBuf) - 1] = '\0';
129-
return errBuf;
130-
}
131-
}
132-
#endif
133101

134102
return "Unknown platform error";
135103
}

0 commit comments

Comments
 (0)