Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r49195 - trunk/boost/asio/ip
From: chris_at_[hidden]
Date: 2008-10-09 02:22:59


Author: chris_kohlhoff
Date: 2008-10-09 02:22:58 EDT (Thu, 09 Oct 2008)
New Revision: 49195
URL: http://svn.boost.org/trac/boost/changeset/49195

Log:
Add missing bounds checks as specified in TR2 proposal.

Text files modified:
   trunk/boost/asio/ip/address_v4.hpp | 19 +++++++++++++++++++
   trunk/boost/asio/ip/address_v6.hpp | 17 ++++++++++++++++-
   2 files changed, 35 insertions(+), 1 deletions(-)

Modified: trunk/boost/asio/ip/address_v4.hpp
==============================================================================
--- trunk/boost/asio/ip/address_v4.hpp (original)
+++ trunk/boost/asio/ip/address_v4.hpp 2008-10-09 02:22:58 EDT (Thu, 09 Oct 2008)
@@ -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 @@
   /// 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 @@
   /// 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);
   }
 

Modified: trunk/boost/asio/ip/address_v6.hpp
==============================================================================
--- trunk/boost/asio/ip/address_v6.hpp (original)
+++ trunk/boost/asio/ip/address_v6.hpp 2008-10-09 02:22:58 EDT (Thu, 09 Oct 2008)
@@ -63,6 +63,17 @@
   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 @@
   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);


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk