Skip to content

Commit c4c482e

Browse files
detuledevbww
andauthored
Fix MUSL CRAN check fail. (#964)
* Simplify the wrapping of strerror_r(). * cctz: fixup windows macro * ccts: fixup windows api * cctz: one more windows api change --------- Co-authored-by: Bradley White <bww@acm.org>
1 parent 32ed591 commit c4c482e

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/cctz/src/time_zone_info.cc

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,29 @@ namespace cctz {
4848

4949
namespace {
5050

51-
// Convert errnum to a message, using buf[buflen] if necessary.
52-
// buf must be non-null, and buflen non-zero.
53-
char* errmsg(int errnum, char* buf, size_t buflen) {
54-
#if defined(_WIN32) || defined(_WIN64)
55-
strerror_s(buf, buflen, errnum);
56-
return buf;
57-
#elif defined(__APPLE__)
58-
strerror_r(errnum, buf, buflen);
51+
// GNU strerror_r() adapter.
52+
template <char* (*strerror_r)(int, char*, std::size_t)>
53+
char* StrError(int errnum, char* buf, std::size_t buflen) {
54+
return strerror_r(errnum, buf, buflen);
55+
}
56+
57+
// XSI strerror_r() adapter.
58+
template <int (*strerror_r)(int, char*, std::size_t)>
59+
char* StrError(int errnum, char* buf, std::size_t buflen) {
60+
if (int e = strerror_r(errnum, buf, buflen))
61+
strerror_r(e < 0 ? EINVAL : e, buf, buflen);
5962
return buf;
60-
#elif ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || __MUSL__
61-
strerror_r(errnum, buf, buflen);
63+
}
64+
65+
// Returns a string describing the error number.
66+
std::string StrError(int errnum) {
67+
char buf[128];
68+
#if defined(_WIN32) || defined(_WIN64)
69+
if (int e = strerror_s(buf, sizeof buf, errnum))
70+
strerror_s(buf, sizeof buf, e);
6271
return buf;
6372
#else
64-
return strerror_r(errnum, buf, buflen);
73+
return StrError<strerror_r>(errnum, buf, sizeof buf);
6574
#endif
6675
}
6776

@@ -684,8 +693,7 @@ bool TimeZoneInfo::Load(const std::string& name) {
684693
loaded = Load(name, fp);
685694
fclose(fp);
686695
} else {
687-
char ebuf[64];
688-
std::clog << path << ": " << errmsg(errno, ebuf, sizeof ebuf) << "\n";
696+
std::clog << path << ": " << StrError(errno) << "\n";
689697
}
690698
return loaded;
691699
}

0 commit comments

Comments
 (0)