diff --git a/include/boost/system/detail/error_category_impl.hpp b/include/boost/system/detail/error_category_impl.hpp index 9ab0e49..66f2f60 100644 --- a/include/boost/system/detail/error_category_impl.hpp +++ b/include/boost/system/detail/error_category_impl.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -83,7 +84,8 @@ inline char const * error_category::message( int ev, char * buffer, std::size_t #if !defined(BOOST_NO_EXCEPTIONS) catch( ... ) { - return "Message text unavailable"; + detail::snprintf( buffer, len, "No message text available for error %d", ev ); + return buffer; } #endif } diff --git a/include/boost/system/detail/snprintf.hpp b/include/boost/system/detail/snprintf.hpp new file mode 100644 index 0000000..d077102 --- /dev/null +++ b/include/boost/system/detail/snprintf.hpp @@ -0,0 +1,70 @@ +#ifndef BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED +#define BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED + +// Copyright 2018, 2020, 2021 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 +#include +#include + +// + +namespace boost +{ + +namespace system +{ + +namespace detail +{ + +#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) ) + +inline void snprintf( char * buffer, std::size_t len, char const * format, ... ) +{ +# if defined( BOOST_MSVC ) +# pragma warning( push ) +# pragma warning( disable: 4996 ) +# endif + + if( len == 0 ) return; + + va_list args; + va_start( args, format ); + + _vsnprintf( buffer, len - 1, format, args ); + buffer[ len - 1 ] = 0; + + va_end( args ); + +# if defined( BOOST_MSVC ) +# pragma warning( pop ) +# endif +} + +#else + +inline void snprintf( char * buffer, std::size_t len, char const * format, ... ) +{ + va_list args; + va_start( args, format ); + + std::vsnprintf( buffer, len, format, args ); + + va_end( args ); +} + +#endif + +} // namespace detail + +} // namespace system + +} // namespace boost + +#endif // #ifndef BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED diff --git a/include/boost/system/detail/system_category_message_win32.hpp b/include/boost/system/detail/system_category_message_win32.hpp index 6cdec2d..e5f544c 100644 --- a/include/boost/system/detail/system_category_message_win32.hpp +++ b/include/boost/system/detail/system_category_message_win32.hpp @@ -10,6 +10,7 @@ // // See library home page at http://www.boost.org/libs/system +#include #include #include #include @@ -27,35 +28,12 @@ namespace system namespace detail { -#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) ) - inline char const * unknown_message_win32( int ev, char * buffer, std::size_t len ) { -# if defined( BOOST_MSVC ) -# pragma warning( push ) -# pragma warning( disable: 4996 ) -# endif - - _snprintf( buffer, len - 1, "Unknown error (%d)", ev ); - - buffer[ len - 1 ] = 0; - return buffer; - -# if defined( BOOST_MSVC ) -# pragma warning( pop ) -# endif -} - -#else - -inline char const * unknown_message_win32( int ev, char * buffer, std::size_t len ) -{ - std::snprintf( buffer, len, "Unknown error (%d)", ev ); + detail::snprintf( buffer, len, "Unknown error (%d)", ev ); return buffer; } -#endif - inline boost::winapi::UINT_ message_cp_win32() { #if defined(BOOST_SYSTEM_USE_UTF8) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 23b0b17..4dd3ab8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -87,3 +87,5 @@ boost_test(TYPE run SOURCES cygwin_error_test.cpp) boost_test(TYPE run SOURCES linux_error_test.cpp) boost_test(TYPE link SOURCES errc_test3.cpp) + +boost_test(TYPE run SOURCES snprintf_test.cpp) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 0998571..3061abe 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -91,3 +91,5 @@ run cygwin_error_test.cpp ; run linux_error_test.cpp ; link errc_test3.cpp ; + +run snprintf_test.cpp ; diff --git a/test/snprintf_test.cpp b/test/snprintf_test.cpp new file mode 100644 index 0000000..02e6f75 --- /dev/null +++ b/test/snprintf_test.cpp @@ -0,0 +1,60 @@ +// Copyright 2021 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include + +int main() +{ + { + char buffer[ 64 ]; + boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 ); + + BOOST_TEST_CSTR_EQ( buffer, "...xy...151..." ); + } + + { + char buffer[ 64 ]; + boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 ); + + BOOST_TEST_CSTR_EQ( buffer, "...xy...151..." ); + } + + { + char buffer[ 15 ]; + boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 ); + + BOOST_TEST_CSTR_EQ( buffer, "...xy...151..." ); + } + + { + char buffer[ 14 ]; + boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 ); + + BOOST_TEST_CSTR_EQ( buffer, "...xy...151.." ); + } + + { + char buffer[ 5 ]; + boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 ); + + BOOST_TEST_CSTR_EQ( buffer, "...x" ); + } + + { + char buffer[ 1 ]; + boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 ); + + BOOST_TEST_CSTR_EQ( buffer, "" ); + } + + { + char buffer[ 1 ] = { 'Q' }; + boost::system::detail::snprintf( buffer, 0, "...%s...%d...", "xy", 151 ); + + BOOST_TEST_EQ( buffer[0], 'Q' ); + } + + return boost::report_errors(); +}