From 7fdff949971895619bc963c233d914424bb716fc Mon Sep 17 00:00:00 2001 From: Christopher Kohlhoff Date: Wed, 7 Dec 2022 07:34:10 +1100 Subject: [PATCH] Use snprintf rather than sprintf on latest Xcode. --- include/boost/asio/detail/config.hpp | 11 +++++++++++ include/boost/asio/detail/impl/handler_tracking.ipp | 4 +++- include/boost/asio/detail/impl/socket_ops.ipp | 12 ++++++++++-- include/boost/asio/ip/impl/network_v4.ipp | 4 +++- include/boost/asio/ip/impl/network_v6.ipp | 4 +++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/boost/asio/detail/config.hpp b/include/boost/asio/detail/config.hpp index f929929e..6abf3d66 100644 --- a/include/boost/asio/detail/config.hpp +++ b/include/boost/asio/detail/config.hpp @@ -2193,4 +2193,15 @@ # endif // !defined(BOOST_ASIO_DISABLE_STD_TO_ADDRESS) #endif // !defined(BOOST_ASIO_HAS_STD_TO_ADDRESS) +// Standard library support for snprintf. +#if !defined(BOOST_ASIO_HAS_SNPRINTF) +# if !defined(BOOST_ASIO_DISABLE_SNPRINTF) +# if defined(__apple_build_version__) +# if (__clang_major__ >= 14) +# define BOOST_ASIO_HAS_SNPRINTF 1 +# endif // (__clang_major__ >= 14) +# endif // defined(__apple_build_version__) +# endif // !defined(BOOST_ASIO_DISABLE_SNPRINTF) +#endif // !defined(BOOST_ASIO_HAS_SNPRINTF) + #endif // BOOST_ASIO_DETAIL_CONFIG_HPP diff --git a/include/boost/asio/detail/impl/handler_tracking.ipp b/include/boost/asio/detail/impl/handler_tracking.ipp index 8d3da01a..95c16441 100644 --- a/include/boost/asio/detail/impl/handler_tracking.ipp +++ b/include/boost/asio/detail/impl/handler_tracking.ipp @@ -366,7 +366,9 @@ void handler_tracking::write_line(const char* format, ...) va_start(args, format); char line[256] = ""; -#if defined(BOOST_ASIO_HAS_SECURE_RTL) +#if defined(BOOST_ASIO_HAS_SNPRINTF) + int length = vsnprintf(line, sizeof(line), format, args); +#elif defined(BOOST_ASIO_HAS_SECURE_RTL) int length = vsprintf_s(line, sizeof(line), format, args); #else // defined(BOOST_ASIO_HAS_SECURE_RTL) int length = vsprintf(line, format, args); diff --git a/include/boost/asio/detail/impl/socket_ops.ipp b/include/boost/asio/detail/impl/socket_ops.ipp index 89cbcaa9..89be2f06 100644 --- a/include/boost/asio/detail/impl/socket_ops.ipp +++ b/include/boost/asio/detail/impl/socket_ops.ipp @@ -2520,7 +2520,11 @@ const char* inet_ntop(int af, const void* src, char* dest, size_t length, && ((ipv6_address->s6_addr[1] & 0x0f) == 0x02)); if ((!is_link_local && !is_multicast_link_local) || if_indextoname(static_cast(scope_id), if_name + 1) == 0) +#if defined(BOOST_ASIO_HAS_SNPRINTF) + snprintf(if_name + 1, sizeof(if_name) - 1, "%lu", scope_id); +#else // defined(BOOST_ASIO_HAS_SNPRINTF) sprintf(if_name + 1, "%lu", scope_id); +#endif // defined(BOOST_ASIO_HAS_SNPRINTF) strcat(dest, if_name); } return result; @@ -3628,7 +3632,9 @@ inline boost::system::error_code getnameinfo_emulation( { return ec = boost::asio::error::no_buffer_space; } -#if defined(BOOST_ASIO_HAS_SECURE_RTL) +#if defined(BOOST_ASIO_HAS_SNPRINTF) + snprintf(serv, servlen, "%u", ntohs(port)); +#elif defined(BOOST_ASIO_HAS_SECURE_RTL) sprintf_s(serv, servlen, "%u", ntohs(port)); #else // defined(BOOST_ASIO_HAS_SECURE_RTL) sprintf(serv, "%u", ntohs(port)); @@ -3651,7 +3657,9 @@ inline boost::system::error_code getnameinfo_emulation( { return ec = boost::asio::error::no_buffer_space; } -#if defined(BOOST_ASIO_HAS_SECURE_RTL) +#if defined(BOOST_ASIO_HAS_SNPRINTF) + snprintf(serv, servlen, "%u", ntohs(port)); +#elif defined(BOOST_ASIO_HAS_SECURE_RTL) sprintf_s(serv, servlen, "%u", ntohs(port)); #else // defined(BOOST_ASIO_HAS_SECURE_RTL) sprintf(serv, "%u", ntohs(port)); diff --git a/include/boost/asio/ip/impl/network_v4.ipp b/include/boost/asio/ip/impl/network_v4.ipp index 9d7d1fbf..7ac14253 100644 --- a/include/boost/asio/ip/impl/network_v4.ipp +++ b/include/boost/asio/ip/impl/network_v4.ipp @@ -130,7 +130,9 @@ std::string network_v4::to_string(boost::system::error_code& ec) const using namespace std; // For sprintf. ec = boost::system::error_code(); char prefix_len[16]; -#if defined(BOOST_ASIO_HAS_SECURE_RTL) +#if defined(BOOST_ASIO_HAS_SNPRINTF) + snprintf(prefix_len, sizeof(prefix_len), "/%u", prefix_length_); +#elif defined(BOOST_ASIO_HAS_SECURE_RTL) sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_); #else // defined(BOOST_ASIO_HAS_SECURE_RTL) sprintf(prefix_len, "/%u", prefix_length_); diff --git a/include/boost/asio/ip/impl/network_v6.ipp b/include/boost/asio/ip/impl/network_v6.ipp index ce189c1b..5f96dd53 100644 --- a/include/boost/asio/ip/impl/network_v6.ipp +++ b/include/boost/asio/ip/impl/network_v6.ipp @@ -99,7 +99,9 @@ std::string network_v6::to_string(boost::system::error_code& ec) const using namespace std; // For sprintf. ec = boost::system::error_code(); char prefix_len[16]; -#if defined(BOOST_ASIO_HAS_SECURE_RTL) +#if defined(BOOST_ASIO_HAS_SNPRINTF) + snprintf(prefix_len, sizeof(prefix_len), "/%u", prefix_length_); +#elif defined(BOOST_ASIO_HAS_SECURE_RTL) sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_); #else // defined(BOOST_ASIO_HAS_SECURE_RTL) sprintf(prefix_len, "/%u", prefix_length_);