Vendor import of llvm-project branch release/19.x llvmorg-19.1.1-0-gd401987fe349...
[freebsd/src.git] / libcxx / src / system_error.cpp
blobf518b480a27820797683f9de7d493410e5931a76
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include <__assert>
10 #include <__config>
11 #include <__verbose_abort>
12 #include <cerrno>
13 #include <cstdio>
14 #include <cstdlib>
15 #include <cstring>
16 #include <string.h>
17 #include <string>
18 #include <system_error>
20 #include "include/config_elast.h"
22 #if defined(__ANDROID__)
23 # include <android/api-level.h>
24 #endif
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 namespace {
29 #if !defined(_LIBCPP_HAS_NO_THREADS)
31 // GLIBC also uses 1024 as the maximum buffer size internally.
32 constexpr size_t strerror_buff_size = 1024;
34 string do_strerror_r(int ev);
36 # if defined(_LIBCPP_MSVCRT_LIKE)
37 string do_strerror_r(int ev) {
38 char buffer[strerror_buff_size];
39 if (::strerror_s(buffer, strerror_buff_size, ev) == 0)
40 return string(buffer);
41 std::snprintf(buffer, strerror_buff_size, "unknown error %d", ev);
42 return string(buffer);
44 # else
46 // Only one of the two following functions will be used, depending on
47 // the return type of strerror_r:
49 // For the GNU variant, a char* return value:
50 __attribute__((unused)) const char* handle_strerror_r_return(char* strerror_return, char* buffer) {
51 // GNU always returns a string pointer in its return value. The
52 // string might point to either the input buffer, or a static
53 // buffer, but we don't care which.
54 return strerror_return;
57 // For the POSIX variant: an int return value.
58 __attribute__((unused)) const char* handle_strerror_r_return(int strerror_return, char* buffer) {
59 // The POSIX variant either:
60 // - fills in the provided buffer and returns 0
61 // - returns a positive error value, or
62 // - returns -1 and fills in errno with an error value.
63 if (strerror_return == 0)
64 return buffer;
66 // Only handle EINVAL. Other errors abort.
67 int new_errno = strerror_return == -1 ? errno : strerror_return;
68 if (new_errno == EINVAL)
69 return "";
71 _LIBCPP_ASSERT_INTERNAL(new_errno == ERANGE, "unexpected error from ::strerror_r");
72 // FIXME maybe? 'strerror_buff_size' is likely to exceed the
73 // maximum error size so ERANGE shouldn't be returned.
74 std::abort();
77 // This function handles both GNU and POSIX variants, dispatching to
78 // one of the two above functions.
79 string do_strerror_r(int ev) {
80 char buffer[strerror_buff_size];
81 // Preserve errno around the call. (The C++ standard requires that
82 // system_error functions not modify errno).
83 const int old_errno = errno;
84 const char* error_message = handle_strerror_r_return(::strerror_r(ev, buffer, strerror_buff_size), buffer);
85 // If we didn't get any message, print one now.
86 if (!error_message[0]) {
87 std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);
88 error_message = buffer;
90 errno = old_errno;
91 return string(error_message);
93 # endif
95 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
97 string make_error_str(const error_code& ec, string what_arg) {
98 if (ec) {
99 if (!what_arg.empty()) {
100 what_arg += ": ";
102 what_arg += ec.message();
104 return what_arg;
107 string make_error_str(const error_code& ec) {
108 if (ec) {
109 return ec.message();
111 return string();
113 } // end namespace
115 string __do_message::message(int ev) const {
116 #if defined(_LIBCPP_HAS_NO_THREADS)
117 return string(::strerror(ev));
118 #else
119 return do_strerror_r(ev);
120 #endif
123 class _LIBCPP_HIDDEN __generic_error_category : public __do_message {
124 public:
125 virtual const char* name() const noexcept;
126 virtual string message(int ev) const;
129 const char* __generic_error_category::name() const noexcept { return "generic"; }
131 string __generic_error_category::message(int ev) const {
132 #ifdef _LIBCPP_ELAST
133 if (ev > _LIBCPP_ELAST)
134 return string("unspecified generic_category error");
135 #endif // _LIBCPP_ELAST
136 return __do_message::message(ev);
139 const error_category& generic_category() noexcept {
140 union AvoidDestroyingGenericCategory {
141 __generic_error_category generic_error_category;
142 constexpr explicit AvoidDestroyingGenericCategory() : generic_error_category() {}
143 ~AvoidDestroyingGenericCategory() {}
145 constinit static AvoidDestroyingGenericCategory helper;
146 return helper.generic_error_category;
149 class _LIBCPP_HIDDEN __system_error_category : public __do_message {
150 public:
151 virtual const char* name() const noexcept;
152 virtual string message(int ev) const;
153 virtual error_condition default_error_condition(int ev) const noexcept;
156 const char* __system_error_category::name() const noexcept { return "system"; }
158 string __system_error_category::message(int ev) const {
159 #ifdef _LIBCPP_ELAST
160 if (ev > _LIBCPP_ELAST)
161 return string("unspecified system_category error");
162 #endif // _LIBCPP_ELAST
163 return __do_message::message(ev);
166 error_condition __system_error_category::default_error_condition(int ev) const noexcept {
167 #ifdef _LIBCPP_ELAST
168 if (ev > _LIBCPP_ELAST)
169 return error_condition(ev, system_category());
170 #endif // _LIBCPP_ELAST
171 return error_condition(ev, generic_category());
174 const error_category& system_category() noexcept {
175 union AvoidDestroyingSystemCategory {
176 __system_error_category system_error_category;
177 constexpr explicit AvoidDestroyingSystemCategory() : system_error_category() {}
178 ~AvoidDestroyingSystemCategory() {}
180 constinit static AvoidDestroyingSystemCategory helper;
181 return helper.system_error_category;
184 // error_condition
186 string error_condition::message() const { return __cat_->message(__val_); }
188 // error_code
190 string error_code::message() const { return __cat_->message(__val_); }
192 // system_error
194 system_error::system_error(error_code ec, const string& what_arg)
195 : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}
197 system_error::system_error(error_code ec, const char* what_arg)
198 : runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}
200 system_error::system_error(error_code ec) : runtime_error(make_error_str(ec)), __ec_(ec) {}
202 system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
203 : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}
205 system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
206 : runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}
208 system_error::system_error(int ev, const error_category& ecat)
209 : runtime_error(make_error_str(error_code(ev, ecat))), __ec_(error_code(ev, ecat)) {}
211 system_error::~system_error() noexcept {}
213 void __throw_system_error(int ev, const char* what_arg) {
214 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
215 std::__throw_system_error(error_code(ev, system_category()), what_arg);
216 #else
217 // The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily.
218 _LIBCPP_VERBOSE_ABORT(
219 "system_error was thrown in -fno-exceptions mode with error %i and message \"%s\"", ev, what_arg);
220 #endif
223 _LIBCPP_END_NAMESPACE_STD