diff --git a/include/boost/asio/ip/address_v4.hpp b/include/boost/asio/ip/address_v4.hpp index 9bea8cbc..357edfae 100644 --- a/include/boost/asio/ip/address_v4.hpp +++ b/include/boost/asio/ip/address_v4.hpp @@ -18,7 +18,9 @@ #include #include +#include #include +#include #include #include #include @@ -56,6 +58,15 @@ public: /// Construct an address from raw bytes. explicit address_v4(const bytes_type& bytes) { +#if UCHAR_MAX > 0xFF + if (bytes[0] > 0xFF || bytes[1] > 0xFF + || bytes[2] > 0xFF || bytes[3] > 0xFF) + { + std::out_of_range ex("address_v4 from bytes_type"); + boost::throw_exception(ex); + } +#endif // UCHAR_MAX > 0xFF + using namespace std; // For memcpy. memcpy(&addr_.s_addr, bytes.elems, 4); } @@ -63,6 +74,14 @@ public: /// Construct an address from a unsigned long in host byte order. explicit address_v4(unsigned long addr) { +#if ULONG_MAX > 0xFFFFFFFF + if (addr > 0xFFFFFFFF) + { + std::out_of_range ex("address_v4 from unsigned long"); + boost::throw_exception(ex); + } +#endif // ULONG_MAX > 0xFFFFFFFF + addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(addr); } diff --git a/include/boost/asio/ip/address_v6.hpp b/include/boost/asio/ip/address_v6.hpp index 89f2ee3f..5f5f0927 100644 --- a/include/boost/asio/ip/address_v6.hpp +++ b/include/boost/asio/ip/address_v6.hpp @@ -63,6 +63,17 @@ public: explicit address_v6(const bytes_type& bytes, unsigned long scope_id = 0) : scope_id_(scope_id) { +#if UCHAR_MAX > 0xFF + for (std::size_t i = 0; i < bytes.size(); ++i) + { + if (bytes[i] > 0xFF) + { + std::out_of_range ex("address_v6 from bytes_type"); + boost::throw_exception(ex); + } + } +#endif // UCHAR_MAX > 0xFF + using namespace std; // For memcpy. memcpy(addr_.s6_addr, bytes.elems, 16); } @@ -166,7 +177,11 @@ public: address_v4 to_v4() const { if (!is_v4_mapped() && !is_v4_compatible()) - throw std::bad_cast(); + { + std::bad_cast ex; + boost::throw_exception(ex); + } + address_v4::bytes_type v4_bytes = { { addr_.s6_addr[12], addr_.s6_addr[13], addr_.s6_addr[14], addr_.s6_addr[15] } }; return address_v4(v4_bytes);