diff options
author | Taku Kudo <taku@google.com> | 2018-06-20 04:37:28 +0300 |
---|---|---|
committer | Taku Kudo <taku@google.com> | 2018-06-20 04:37:28 +0300 |
commit | 23959e3c94d543e18b2c119b990a4b47043724e8 (patch) | |
tree | bbd88ed9dfe205f0a9bd22e0a8fa5b04b4089c58 /src | |
parent | 0fd7f6b33188434c63b164cd46e6c831419065d3 (diff) |
Fixes the usage of strerror_r
Diffstat (limited to 'src')
-rw-r--r-- | src/util.cc | 19 | ||||
-rw-r--r-- | src/util.h | 6 |
2 files changed, 20 insertions, 5 deletions
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 @@ -416,11 +416,7 @@ void STLDeleteElements(std::vector<T *> *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(); } |