Use strerror_r on glibc

This commit is contained in:
Peter Dimov
2018-09-18 22:21:13 +03:00
parent 7d38263d71
commit 515fbb21b4
2 changed files with 78 additions and 29 deletions

View File

@ -0,0 +1,69 @@
// Implementation details of generic_error_category
//
// Copyright 2018 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See library home page at http://www.boost.org/libs/system
#include <cstring>
namespace boost
{
namespace system
{
namespace detail
{
#if defined(__GLIBC__)
// std::strerror is not thread-safe on glibc (for no reason)
// glibc also has two incompatible strerror_r definitions (for no reason)
inline char const * strerror_r_helper( char const * r, char const * )
{
return r;
}
inline char const * strerror_r_helper( int r, char const * buffer )
{
return r == 0? buffer: "Unknown error";
}
inline std::string generic_error_category_message( int ev )
{
char buffer[ 128 ];
return strerror_r_helper( strerror_r( ev, buffer, sizeof( buffer ) ), buffer );
}
#else
// std::strerror is thread-safe on everything else, incl. Windows
inline std::string generic_error_category_message( int ev )
{
# if defined( BOOST_MSVC )
# pragma warning( push )
# pragma warning( disable: 4996 )
# endif
char const * m = std::strerror( ev );
# if defined( BOOST_MSVC )
# pragma warning( pop )
# endif
return m? m: "Unknown error";
#endif
}
} // namespace detail
} // namespace system
} // namespace boost

View File

@ -17,7 +17,6 @@
#include <ostream>
#include <string>
#include <functional>
#include <cstring>
// TODO: undef these macros if not already defined
#include <boost/cerrno.hpp>
@ -761,38 +760,19 @@ inline bool error_category::equivalent( const error_code & code, int condition )
return *this == code.category() && code.value() == condition;
}
// generic_error_category implementation
namespace detail
{
inline char const * generic_error_category_message( int ev )
{
#ifdef BOOST_MSVC
#pragma warning( push )
#pragma warning( disable: 4996 )
#endif
char const * m = std::strerror( ev );
#ifdef BOOST_MSVC
#pragma warning( pop )
#endif
return m? m: "Unknown error";
}
inline std::string generic_error_category::message( int ev ) const
{
return generic_error_category_message( ev );
}
} // namespace detail
} // namespace system
} // namespace boost
// generic_error_category implementation
#include <boost/system/detail/generic_category.hpp>
inline std::string boost::system::detail::generic_error_category::message( int ev ) const
{
return generic_error_category_message( ev );
}
// system_error_category implementation
#if defined(BOOST_WINDOWS_API)