From 83b85371785bae2928332ac758f2359b724ef420 Mon Sep 17 00:00:00 2001 From: Jose Date: Fri, 27 Jul 2018 13:55:58 +0200 Subject: Replace strerror usage with IceUtilInternal::errorToString Close #154 --- cpp/src/IceUtil/StringUtil.cpp | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'cpp/src/IceUtil/StringUtil.cpp') diff --git a/cpp/src/IceUtil/StringUtil.cpp b/cpp/src/IceUtil/StringUtil.cpp index c1c9099e215..3ecbe9ebd99 100644 --- a/cpp/src/IceUtil/StringUtil.cpp +++ b/cpp/src/IceUtil/StringUtil.cpp @@ -10,7 +10,9 @@ #include #include #include +#include // for strerror_r +#include #include using namespace std; @@ -1071,7 +1073,38 @@ IceUtilInternal::lastErrorToString() string IceUtilInternal::errorToString(int error) { - return strerror(error); + char buf[500]; +#if !defined(__GLIBC__) || defined(BSD) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) + // + // Use the XSI-compliant version of strerror_r + // + if(strerror_r(error, &buf[0], 500) != 0) + { + ostringstream os; + os << "Unknown error `" << error << "'"; + return os.str(); + } + else + { + return string(buf); + } +#else + // + // Use the GNU-specific version of strerror_r + // + errno = 0; + const char* msg = strerror_r(error, &buf[0], 500); + if(errno != 0) + { + ostringstream os; + os << "Unknown error `" << error << "'"; + return os.str(); + } + else + { + return msg; + } +#endif } string -- cgit v1.2.3