1 //===---------------------- system_error.cpp ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "system_error"
14 #include "include/config_elast.h"
23 #if defined(__ANDROID__)
24 #include <android/api-level.h>
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 // class error_category
31 #if defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
32 error_category::error_category() _NOEXCEPT
37 error_category::~error_category() _NOEXCEPT
42 error_category::default_error_condition(int ev) const _NOEXCEPT
44 return error_condition(ev, *this);
48 error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
50 return default_error_condition(code) == condition;
54 error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
56 return *this == code.category() && code.value() == condition;
59 #if !defined(_LIBCPP_HAS_NO_THREADS)
62 // GLIBC also uses 1024 as the maximum buffer size internally.
63 constexpr size_t strerror_buff_size = 1024;
65 string do_strerror_r(int ev);
67 #if defined(_LIBCPP_MSVCRT_LIKE)
68 string do_strerror_r(int ev) {
69 char buffer[strerror_buff_size];
70 if (::strerror_s(buffer, strerror_buff_size, ev) == 0)
71 return string(buffer);
72 std::snprintf(buffer, strerror_buff_size, "unknown error %d", ev);
73 return string(buffer);
77 // Only one of the two following functions will be used, depending on
78 // the return type of strerror_r:
80 // For the GNU variant, a char* return value:
81 __attribute__((unused)) const char *
82 handle_strerror_r_return(char *strerror_return, char *buffer) {
83 // GNU always returns a string pointer in its return value. The
84 // string might point to either the input buffer, or a static
85 // buffer, but we don't care which.
86 return strerror_return;
89 // For the POSIX variant: an int return value.
90 __attribute__((unused)) const char *
91 handle_strerror_r_return(int strerror_return, char *buffer) {
92 // The POSIX variant either:
93 // - fills in the provided buffer and returns 0
94 // - returns a positive error value, or
95 // - returns -1 and fills in errno with an error value.
96 if (strerror_return == 0)
99 // Only handle EINVAL. Other errors abort.
100 int new_errno = strerror_return == -1 ? errno : strerror_return;
101 if (new_errno == EINVAL)
104 _LIBCPP_ASSERT(new_errno == ERANGE, "unexpected error from ::strerror_r");
105 // FIXME maybe? 'strerror_buff_size' is likely to exceed the
106 // maximum error size so ERANGE shouldn't be returned.
110 // This function handles both GNU and POSIX variants, dispatching to
111 // one of the two above functions.
112 string do_strerror_r(int ev) {
113 char buffer[strerror_buff_size];
114 // Preserve errno around the call. (The C++ standard requires that
115 // system_error functions not modify errno).
116 const int old_errno = errno;
117 const char *error_message = handle_strerror_r_return(
118 ::strerror_r(ev, buffer, strerror_buff_size), buffer);
119 // If we didn't get any message, print one now.
120 if (!error_message[0]) {
121 std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);
122 error_message = buffer;
125 return string(error_message);
132 __do_message::message(int ev) const
134 #if defined(_LIBCPP_HAS_NO_THREADS)
135 return string(::strerror(ev));
137 return do_strerror_r(ev);
141 class _LIBCPP_HIDDEN __generic_error_category
142 : public __do_message
145 virtual const char* name() const _NOEXCEPT;
146 virtual string message(int ev) const;
150 __generic_error_category::name() const _NOEXCEPT
156 __generic_error_category::message(int ev) const
159 if (ev > _LIBCPP_ELAST)
160 return string("unspecified generic_category error");
161 #endif // _LIBCPP_ELAST
162 return __do_message::message(ev);
165 const error_category&
166 generic_category() _NOEXCEPT
168 static __generic_error_category s;
172 class _LIBCPP_HIDDEN __system_error_category
173 : public __do_message
176 virtual const char* name() const _NOEXCEPT;
177 virtual string message(int ev) const;
178 virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
182 __system_error_category::name() const _NOEXCEPT
188 __system_error_category::message(int ev) const
191 if (ev > _LIBCPP_ELAST)
192 return string("unspecified system_category error");
193 #endif // _LIBCPP_ELAST
194 return __do_message::message(ev);
198 __system_error_category::default_error_condition(int ev) const _NOEXCEPT
201 if (ev > _LIBCPP_ELAST)
202 return error_condition(ev, system_category());
203 #endif // _LIBCPP_ELAST
204 return error_condition(ev, generic_category());
207 const error_category&
208 system_category() _NOEXCEPT
210 static __system_error_category s;
217 error_condition::message() const
219 return __cat_->message(__val_);
225 error_code::message() const
227 return __cat_->message(__val_);
233 system_error::__init(const error_code& ec, string what_arg)
237 if (!what_arg.empty())
239 what_arg += ec.message();
244 system_error::system_error(error_code ec, const string& what_arg)
245 : runtime_error(__init(ec, what_arg)),
250 system_error::system_error(error_code ec, const char* what_arg)
251 : runtime_error(__init(ec, what_arg)),
256 system_error::system_error(error_code ec)
257 : runtime_error(__init(ec, "")),
262 system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
263 : runtime_error(__init(error_code(ev, ecat), what_arg)),
264 __ec_(error_code(ev, ecat))
268 system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
269 : runtime_error(__init(error_code(ev, ecat), what_arg)),
270 __ec_(error_code(ev, ecat))
274 system_error::system_error(int ev, const error_category& ecat)
275 : runtime_error(__init(error_code(ev, ecat), "")),
276 __ec_(error_code(ev, ecat))
280 system_error::~system_error() _NOEXCEPT
285 __throw_system_error(int ev, const char* what_arg)
287 #ifndef _LIBCPP_NO_EXCEPTIONS
288 throw system_error(error_code(ev, system_category()), what_arg);
296 _LIBCPP_END_NAMESPACE_STD