Add bounds checks as specified in TR2 proposal.
This commit is contained in:
parent
89428b5586
commit
a3fec3a669
@ -18,7 +18,9 @@
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <boost/array.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
@ -55,6 +57,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);
|
||||
}
|
||||
@ -62,6 +73,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 = asio::detail::socket_ops::host_to_network_long(addr);
|
||||
}
|
||||
|
||||
|
@ -62,6 +62,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);
|
||||
}
|
||||
@ -165,7 +176,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);
|
||||
|
Loading…
Reference in New Issue
Block a user