summaryrefslogtreecommitdiff
path: root/contrib/libc++/src/system_error.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2016-08-30 18:27:31 +0000
committerDimitry Andric <dim@FreeBSD.org>2016-08-30 18:27:31 +0000
commit7c82a1ece4c97ca136a699a03aa193cca88f0a1e (patch)
tree052e69c58e6e832f3aa79ea12ac2b727877aa2fc /contrib/libc++/src/system_error.cpp
parentcc5e1c7d548784d1c8ce01591920b7283be48dd3 (diff)
parente947f967d1e30b3973f8a789e682da57481a5ece (diff)
Notes
Diffstat (limited to 'contrib/libc++/src/system_error.cpp')
-rw-r--r--contrib/libc++/src/system_error.cpp57
1 files changed, 56 insertions, 1 deletions
diff --git a/contrib/libc++/src/system_error.cpp b/contrib/libc++/src/system_error.cpp
index 3023e200aa34..87f35ae37f39 100644
--- a/contrib/libc++/src/system_error.cpp
+++ b/contrib/libc++/src/system_error.cpp
@@ -13,8 +13,17 @@
#include "system_error"
#include "include/config_elast.h"
+#include "cerrno"
#include "cstring"
+#include "cstdio"
+#include "cstdlib"
+#include "cassert"
#include "string"
+#include "string.h"
+
+#if defined(__ANDROID__)
+#include <android/api-level.h>
+#endif
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -46,10 +55,56 @@ error_category::equivalent(const error_code& code, int condition) const _NOEXCEP
return *this == code.category() && code.value() == condition;
}
+namespace {
+
+// GLIBC also uses 1024 as the maximum buffer size internally.
+constexpr size_t strerror_buff_size = 1024;
+
+string do_strerror_r(int ev);
+
+#if defined(__linux__) && !defined(_LIBCPP_HAS_MUSL_LIBC) \
+ && (!defined(__ANDROID__) || __ANDROID_API__ >= 23)
+// GNU Extended version
+string do_strerror_r(int ev) {
+ char buffer[strerror_buff_size];
+ char* ret = ::strerror_r(ev, buffer, strerror_buff_size);
+ return string(ret);
+}
+#else
+// POSIX version
+string do_strerror_r(int ev) {
+ char buffer[strerror_buff_size];
+ const int old_errno = errno;
+ int ret;
+ if ((ret = ::strerror_r(ev, buffer, strerror_buff_size)) != 0) {
+ // If `ret == -1` then the error is specified using `errno`, otherwise
+ // `ret` represents the error.
+ const int new_errno = ret == -1 ? errno : ret;
+ errno = old_errno;
+ if (new_errno == EINVAL) {
+ std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);
+ return string(buffer);
+ } else {
+ assert(new_errno == ERANGE);
+ // FIXME maybe? 'strerror_buff_size' is likely to exceed the
+ // maximum error size so ERANGE shouldn't be returned.
+ std::abort();
+ }
+ }
+ return string(buffer);
+}
+#endif
+
+} // end namespace
+
string
__do_message::message(int ev) const
{
- return string(strerror(ev));
+#if defined(_LIBCPP_HAS_NO_THREADS)
+ return string(::strerror(ev));
+#else
+ return do_strerror_r(ev);
+#endif
}
class _LIBCPP_HIDDEN __generic_error_category