feat: use thread-safe variant of strerror

This commit is contained in:
ToruNiina 2023-10-11 01:08:12 +09:00
parent 1beb391a43
commit 22d22198ec

View File

@ -11,11 +11,57 @@
namespace toml namespace toml
{ {
namespace detail
{
inline std::string str_error(int errnum)
{
// C++ standard strerror is not thread-safe.
// C11 provides thread-safe version of this function, `strerror_s`, but it
// is not available in C++.
// To avoid using std::strerror, we need to use platform-specific functions.
// If none of the conditions are met, it calls std::strerror as a fallback.
#ifdef _MSC_VER // MSVC
constexpr std::size_t bufsize = 256;
std::array<char, bufsize> buf;
const auto result = strerror_s(buf.data(), bufsize, errnum);
if(result != 0)
{
return std::string("strerror_s failed");
}
else
{
return std::string(buf.data());
}
#elif (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE // XSI
constexpr std::size_t bufsize = 256;
std::array<char, bufsize> buf;
const int result = strerror_r(errnum, buf.data(), bufsize);
if (result != 0)
{
return std::string("strerror_r failed");
}
else
{
return std::string(buf.data());
}
#elif defined(_GNU_SOURCE) // GNU extension
constexpr std::size_t bufsize = 256;
std::array<char, bufsize> buf;
const char* result = strerror_r(errnum, buf.data(), bufsize);
return std::string(result);
#else // fallback
return std::strerror(errnum);
#endif
}
} // detail
struct file_io_error : public std::runtime_error struct file_io_error : public std::runtime_error
{ {
public: public:
file_io_error(int errnum, const std::string& msg, const std::string& fname) file_io_error(int errnum, const std::string& msg, const std::string& fname)
: std::runtime_error(msg + " \"" + fname + "\": " + std::strerror(errnum)), : std::runtime_error(msg + " \"" + fname + "\": " + detail::str_error(errnum)),
errno_(errnum) errno_(errnum)
{} {}
int get_errno() const noexcept {return errno_;} int get_errno() const noexcept {return errno_;}