From 23959e3c94d543e18b2c119b990a4b47043724e8 Mon Sep 17 00:00:00 2001 From: Taku Kudo Date: Wed, 20 Jun 2018 10:37:28 +0900 Subject: Fixes the usage of strerror_r --- src/util.cc | 19 +++++++++++++++++++ src/util.h | 6 +----- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/util.cc b/src/util.cc index cbf0d1a..916c52c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -263,4 +263,23 @@ bool OutputBuffer::WriteLine(absl::string_view text) { return Write(text) && Write("\n"); } } // namespace io + +namespace util { + +std::string StrError(int errnum) { + constexpr int kStrErrorSize = 1024; + char buffer[kStrErrorSize]; + char *str = nullptr; +#if defined(__GLIBC__) && defined(_GNU_SOURCE) + str = strerror_r(errnum, buffer, kStrErrorSize - 1); +#else + strerror_r(errnum, buffer, kStrErrorSize - 1); + str = buffer; +#endif + std::ostringstream os; + os << str << " Error #" << errnum; + return os.str(); +} + +} // namespace util } // namespace sentencepiece diff --git a/src/util.h b/src/util.h index e9d2d2f..e2af6e9 100644 --- a/src/util.h +++ b/src/util.h @@ -416,11 +416,7 @@ void STLDeleteElements(std::vector *vec) { namespace util { -inline const std::string StrError(int n) { - char buf[1024]; - strerror_r(n, buf, sizeof(buf) - 1); - return std::string(buf); -} +std::string StrError(int errnum); inline Status OkStatus() { return Status(); } -- cgit v1.2.3