Index: src/system_error.cpp =================================================================== --- src/system_error.cpp +++ src/system_error.cpp @@ -13,8 +13,13 @@ #include "system_error" #include "include/config_elast.h" +#include "cerrno" #include "cstring" +#include "cstdio" +#include "cstdlib" +#include "cassert" #include "string" +#include "string.h" _LIBCPP_BEGIN_NAMESPACE_STD @@ -46,10 +51,50 @@ return *this == code.category() && code.value() == condition; } +namespace { + +string do_strerror_r(int ev); + +#if defined(__linux__) && !defined(_LIBCPP_HAS_MUSL_LIBC) +// GNU Extended version +string do_strerror_r(int ev) { + char buffer[1024]; + char* ret = ::strerror_r(ev, buffer, 1024); + return string(ret); +} +#else +// POSIX version +string do_strerror_r(int ev) { + char buffer[1024]; + const int old_errno = errno; + if (::strerror_r(ev, buffer, 1024) == -1) { + const int new_errno = errno; + errno = old_errno; + if (new_errno == EINVAL) { + std::snprintf(buffer, 1024, "Unknown error %d", ev); + return string(buffer); + } else { + assert(new_errno == ERANGE); + // FIXME maybe? 1024 is likely to exceed the maximum error + // size so ERANGE shouldn't be returned. GLIBC also uses 1024 + // as the maximum buffer size internally. + std::abort(); + } + } + return string(buffer); +} +#endif + +} // end namespace + string __do_message::message(int ev) const { - return string(strerror(ev)); +#if defined(_LIBCPP_HAS_NO_THREADS) + return string(::strerror(ev)); +#else + return do_strerror_r(ev); +#endif } class _LIBCPP_HIDDEN __generic_error_category Index: test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp =================================================================== --- test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp +++ test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp @@ -16,10 +16,21 @@ #include #include #include +#include + +void test_message_leaves_errno_unchanged() { + errno = E2BIG; // something that message will never generate + const std::error_category& e_cat1 = std::generic_category(); + e_cat1.message(-1); + assert(errno == E2BIG); +} int main() { const std::error_category& e_cat1 = std::generic_category(); std::string m1 = e_cat1.name(); assert(m1 == "generic"); + { + test_message_leaves_errno_unchanged(); + } } Index: test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp =================================================================== --- test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp +++ test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp @@ -16,6 +16,15 @@ #include #include #include +#include + + +void test_message_leaves_errno_unchanged() { + errno = E2BIG; // something that message will never generate + const std::error_category& e_cat1 = std::system_category(); + e_cat1.message(-1); + assert(errno == E2BIG); +} int main() { @@ -26,4 +35,7 @@ e_cond = e_cat1.default_error_condition(5000); assert(e_cond.value() == 5000); assert(e_cond.category() == std::system_category()); + { + test_message_leaves_errno_unchanged(); + } }