Path: blob/main/contrib/llvm-project/libcxx/src/system_error.cpp
35147 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <__assert>9#include <__config>10#include <__verbose_abort>11#include <cerrno>12#include <cstdio>13#include <cstdlib>14#include <cstring>15#include <string.h>16#include <string>17#include <system_error>1819#include "include/config_elast.h"2021#if defined(__ANDROID__)22# include <android/api-level.h>23#endif2425_LIBCPP_BEGIN_NAMESPACE_STD2627namespace {28#if !defined(_LIBCPP_HAS_NO_THREADS)2930// GLIBC also uses 1024 as the maximum buffer size internally.31constexpr size_t strerror_buff_size = 1024;3233string do_strerror_r(int ev);3435# if defined(_LIBCPP_MSVCRT_LIKE)36string do_strerror_r(int ev) {37char buffer[strerror_buff_size];38if (::strerror_s(buffer, strerror_buff_size, ev) == 0)39return string(buffer);40std::snprintf(buffer, strerror_buff_size, "unknown error %d", ev);41return string(buffer);42}43# else4445// Only one of the two following functions will be used, depending on46// the return type of strerror_r:4748// For the GNU variant, a char* return value:49__attribute__((unused)) const char* handle_strerror_r_return(char* strerror_return, char* buffer) {50// GNU always returns a string pointer in its return value. The51// string might point to either the input buffer, or a static52// buffer, but we don't care which.53return strerror_return;54}5556// For the POSIX variant: an int return value.57__attribute__((unused)) const char* handle_strerror_r_return(int strerror_return, char* buffer) {58// The POSIX variant either:59// - fills in the provided buffer and returns 060// - returns a positive error value, or61// - returns -1 and fills in errno with an error value.62if (strerror_return == 0)63return buffer;6465// Only handle EINVAL. Other errors abort.66int new_errno = strerror_return == -1 ? errno : strerror_return;67if (new_errno == EINVAL)68return "";6970_LIBCPP_ASSERT_INTERNAL(new_errno == ERANGE, "unexpected error from ::strerror_r");71// FIXME maybe? 'strerror_buff_size' is likely to exceed the72// maximum error size so ERANGE shouldn't be returned.73std::abort();74}7576// This function handles both GNU and POSIX variants, dispatching to77// one of the two above functions.78string do_strerror_r(int ev) {79char buffer[strerror_buff_size];80// Preserve errno around the call. (The C++ standard requires that81// system_error functions not modify errno).82const int old_errno = errno;83const char* error_message = handle_strerror_r_return(::strerror_r(ev, buffer, strerror_buff_size), buffer);84// If we didn't get any message, print one now.85if (!error_message[0]) {86std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);87error_message = buffer;88}89errno = old_errno;90return string(error_message);91}92# endif9394#endif // !defined(_LIBCPP_HAS_NO_THREADS)9596string make_error_str(const error_code& ec, string what_arg) {97if (ec) {98if (!what_arg.empty()) {99what_arg += ": ";100}101what_arg += ec.message();102}103return what_arg;104}105106string make_error_str(const error_code& ec) {107if (ec) {108return ec.message();109}110return string();111}112} // end namespace113114string __do_message::message(int ev) const {115#if defined(_LIBCPP_HAS_NO_THREADS)116return string(::strerror(ev));117#else118return do_strerror_r(ev);119#endif120}121122class _LIBCPP_HIDDEN __generic_error_category : public __do_message {123public:124virtual const char* name() const noexcept;125virtual string message(int ev) const;126};127128const char* __generic_error_category::name() const noexcept { return "generic"; }129130string __generic_error_category::message(int ev) const {131#ifdef _LIBCPP_ELAST132if (ev > _LIBCPP_ELAST)133return string("unspecified generic_category error");134#endif // _LIBCPP_ELAST135return __do_message::message(ev);136}137138const error_category& generic_category() noexcept {139union AvoidDestroyingGenericCategory {140__generic_error_category generic_error_category;141constexpr explicit AvoidDestroyingGenericCategory() : generic_error_category() {}142~AvoidDestroyingGenericCategory() {}143};144constinit static AvoidDestroyingGenericCategory helper;145return helper.generic_error_category;146}147148class _LIBCPP_HIDDEN __system_error_category : public __do_message {149public:150virtual const char* name() const noexcept;151virtual string message(int ev) const;152virtual error_condition default_error_condition(int ev) const noexcept;153};154155const char* __system_error_category::name() const noexcept { return "system"; }156157string __system_error_category::message(int ev) const {158#ifdef _LIBCPP_ELAST159if (ev > _LIBCPP_ELAST)160return string("unspecified system_category error");161#endif // _LIBCPP_ELAST162return __do_message::message(ev);163}164165error_condition __system_error_category::default_error_condition(int ev) const noexcept {166#ifdef _LIBCPP_ELAST167if (ev > _LIBCPP_ELAST)168return error_condition(ev, system_category());169#endif // _LIBCPP_ELAST170return error_condition(ev, generic_category());171}172173const error_category& system_category() noexcept {174union AvoidDestroyingSystemCategory {175__system_error_category system_error_category;176constexpr explicit AvoidDestroyingSystemCategory() : system_error_category() {}177~AvoidDestroyingSystemCategory() {}178};179constinit static AvoidDestroyingSystemCategory helper;180return helper.system_error_category;181}182183// error_condition184185string error_condition::message() const { return __cat_->message(__val_); }186187// error_code188189string error_code::message() const { return __cat_->message(__val_); }190191// system_error192193system_error::system_error(error_code ec, const string& what_arg)194: runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}195196system_error::system_error(error_code ec, const char* what_arg)197: runtime_error(make_error_str(ec, what_arg)), __ec_(ec) {}198199system_error::system_error(error_code ec) : runtime_error(make_error_str(ec)), __ec_(ec) {}200201system_error::system_error(int ev, const error_category& ecat, const string& what_arg)202: runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}203204system_error::system_error(int ev, const error_category& ecat, const char* what_arg)205: runtime_error(make_error_str(error_code(ev, ecat), what_arg)), __ec_(error_code(ev, ecat)) {}206207system_error::system_error(int ev, const error_category& ecat)208: runtime_error(make_error_str(error_code(ev, ecat))), __ec_(error_code(ev, ecat)) {}209210system_error::~system_error() noexcept {}211212void __throw_system_error(int ev, const char* what_arg) {213#ifndef _LIBCPP_HAS_NO_EXCEPTIONS214std::__throw_system_error(error_code(ev, system_category()), what_arg);215#else216// The above could also handle the no-exception case, but for size, avoid referencing system_category() unnecessarily.217_LIBCPP_VERBOSE_ABORT(218"system_error was thrown in -fno-exceptions mode with error %i and message \"%s\"", ev, what_arg);219#endif220}221222_LIBCPP_END_NAMESPACE_STD223224225