2
0
mirror of https://github.com/boostorg/asio.git synced 2026-02-24 14:22:08 +00:00

Add missing bounds checks as specified in TR2 proposal.

[SVN r49195]
This commit is contained in:
Christopher Kohlhoff
2008-10-09 06:22:58 +00:00
parent 32da68bc8e
commit d67d33cd9b
2 changed files with 35 additions and 1 deletions

View File

@@ -18,7 +18,9 @@
#include <boost/asio/detail/push_options.hpp>
#include <boost/asio/detail/push_options.hpp>
#include <climits>
#include <string>
#include <stdexcept>
#include <boost/array.hpp>
#include <boost/throw_exception.hpp>
#include <boost/asio/detail/pop_options.hpp>
@@ -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);
}

View File

@@ -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);