Skip to content

Commit 95e4dca

Browse files
committed
Remove dependency with new header file and check build
1 parent 23f4bb2 commit 95e4dca

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

src/platform/PlatformError.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
#include <lib/core/ErrorStr.h>
2222

23-
#include "../system/StrErrorUtil.h"
2423
#include <string.h>
24+
#include <mutex>
25+
#define CHIP_HAVE_STD_MUTEX 1
2526

2627
namespace chip {
2728
namespace Platform {
@@ -54,9 +55,45 @@ const char * DescribePlatformError(CHIP_ERROR aError)
5455
#else
5556
static char errBuf[128];
5657
#endif // CHIP_SYSTEM_CONFIG_THREAD_LOCAL_STORAGE
57-
const char * res = chip::System::PlatformStrError(lError, errBuf, sizeof(errBuf));
58-
if (res != nullptr)
59-
return res;
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)
61+
{
62+
return errBuf;
63+
}
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+
6097
return "Unknown platform error";
6198
}
6299

src/system/SystemError.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737
#include <lwip/err.h>
3838
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
3939

40-
#include "StrErrorUtil.h"
4140
#include <limits>
4241
#include <stddef.h>
4342
#include <string.h>
43+
#include <mutex>
44+
#define CHIP_HAVE_STD_MUTEX 1
4445

4546
namespace chip {
4647
namespace System {
@@ -91,9 +92,44 @@ DLL_EXPORT const char * DescribeErrorPOSIX(CHIP_ERROR aError)
9192
#else
9293
static char errBuf[128];
9394
#endif // CHIP_SYSTEM_CONFIG_THREAD_LOCAL_STORAGE
94-
const char * res = chip::System::PlatformStrError(lError, errBuf, sizeof(errBuf));
95-
if (res != nullptr)
96-
return res;
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)
98+
{
99+
return errBuf;
100+
}
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
97133

98134
return "Unknown platform error";
99135
}

0 commit comments

Comments
 (0)