Merge pull request #115 from google/sr

Fixes the usage of strerror_r
This commit is contained in:
Taku Kudo 2018-06-20 10:58:29 +09:00 committed by GitHub
commit ed6f832234
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -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

View File

@ -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(); }