From b2478a9ba9ed76d7c858419e3f677cc454591d45 Mon Sep 17 00:00:00 2001 From: Christopher Kohlhoff Date: Mon, 1 Dec 2025 22:47:38 +1100 Subject: [PATCH] Remove _FORTIFY_SOURCE workaround. Check size when processing addrinfo. The previous workaround for _FORTIFY_SOURCE warnings increased the size of all IP endpoint objects. Instead, try fixing the warning at the point where it occurs, by limiting the length memcpy-ed while processing the addrinfo results from getaddrinfo. --- .../boost/asio/ip/basic_resolver_results.hpp | 22 ++++++++++++------- include/boost/asio/ip/detail/endpoint.hpp | 12 ++++------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/include/boost/asio/ip/basic_resolver_results.hpp b/include/boost/asio/ip/basic_resolver_results.hpp index ea97b324..e1ebcbc1 100644 --- a/include/boost/asio/ip/basic_resolver_results.hpp +++ b/include/boost/asio/ip/basic_resolver_results.hpp @@ -133,14 +133,20 @@ public: if (address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET) || address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET6)) { - using namespace std; // For memcpy. - typename InternetProtocol::endpoint endpoint; - endpoint.resize(static_cast(address_info->ai_addrlen)); - memcpy(endpoint.data(), address_info->ai_addr, - address_info->ai_addrlen); - results.values_->push_back( - basic_resolver_entry(endpoint, - actual_host_name, service_name)); + const std::size_t expected_size = + address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET) + ? sizeof(boost::asio::detail::sockaddr_in4_type) + : sizeof(boost::asio::detail::sockaddr_in6_type); + if (address_info->ai_addrlen >= expected_size) + { + using namespace std; // For memcpy. + typename InternetProtocol::endpoint endpoint; + endpoint.resize(expected_size); + memcpy(endpoint.data(), address_info->ai_addr, expected_size); + results.values_->push_back( + basic_resolver_entry(endpoint, + actual_host_name, service_name)); + } } address_info = address_info->ai_next; } diff --git a/include/boost/asio/ip/detail/endpoint.hpp b/include/boost/asio/ip/detail/endpoint.hpp index 8483474b..dc84fd2e 100644 --- a/include/boost/asio/ip/detail/endpoint.hpp +++ b/include/boost/asio/ip/detail/endpoint.hpp @@ -60,13 +60,13 @@ public: // Get the underlying endpoint in the native type. boost::asio::detail::socket_addr_type* data() noexcept { - return &data_.base[0]; + return &data_.base; } // Get the underlying endpoint in the native type. const boost::asio::detail::socket_addr_type* data() const noexcept { - return &data_.base[0]; + return &data_.base; } // Get the underlying size of the endpoint in the native type. @@ -111,7 +111,7 @@ public: // Determine whether the endpoint is IPv4. bool is_v4() const noexcept { - return data_.base[0].sa_family == BOOST_ASIO_OS_DEF(AF_INET); + return data_.base.sa_family == BOOST_ASIO_OS_DEF(AF_INET); } #if !defined(BOOST_ASIO_NO_IOSTREAM) @@ -123,11 +123,7 @@ private: // The underlying IP socket address. union data_union { -#if defined(_FORTIFY_SOURCE) - boost::asio::detail::socket_addr_type base[8]; -#else // defined(_FORTIFY_SOURCE) - boost::asio::detail::socket_addr_type base[1]; -#endif // defined(_FORTIFY_SOURCE) + boost::asio::detail::socket_addr_type base; boost::asio::detail::sockaddr_in4_type v4; boost::asio::detail::sockaddr_in6_type v6; } data_;