Fix off-by-2 errors
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 28 May 2008 13:03:29 -0700
changeset 3167 644a220ad981
parent 3166 f65af41d30db
child 3168 c7d239e85b64
Fix off-by-2 errors
src/node/address.cc
src/node/address.h
src/node/socket.cc
--- a/src/node/address.cc	Wed May 28 11:28:06 2008 -0700
+++ b/src/node/address.cc	Wed May 28 13:03:29 2008 -0700
@@ -114,21 +114,20 @@
 }
 
 void
-Address::Serialize (uint8_t* buf, uint32_t len) const
+Address::Serialize (TagBuffer buffer) const
 {
-  NS_ASSERT (len >= static_cast<uint32_t> (m_len + 2));
-  buf[0] = m_type;
-  buf[1] = m_len;
-  for (uint8_t i = 0; i < m_len; i++)
-    {
-      buf[i+2] = m_data[i];
-    }
+  buffer.WriteU8 (m_type);
+  buffer.WriteU8 (m_len);
+  buffer.Write (m_data,  m_len);
 }
 
-Address
-Address::Deserialize (const uint8_t* buf)
+void
+Address::Deserialize (TagBuffer buffer)
 {
-  return Address (buf[0], buf + 2, buf[1]);
+  m_type = buffer.ReadU8 ();
+  m_len = buffer.ReadU8 ();
+  NS_ASSERT (m_len <= MAX_SIZE);
+  buffer.Read (m_data, m_len);
 }
 
 ATTRIBUTE_HELPER_CPP (Address);
--- a/src/node/address.h	Wed May 28 11:28:06 2008 -0700
+++ b/src/node/address.h	Wed May 28 13:03:29 2008 -0700
@@ -5,6 +5,7 @@
 #include <ostream>
 #include "ns3/attribute.h"
 #include "ns3/attribute-helper.h"
+#include "ns3/tag-buffer.h"
 
 namespace ns3 {
 
@@ -166,14 +167,14 @@
    * \param buf output buffer that gets written with this Address
    * \param len length of output buffer
    */
-  void Serialize (uint8_t* buf, uint32_t len) const;
+  void Serialize (TagBuffer buffer) const;
   /**
    * \param buf buffer to read address from
    * \returns an Address
    * 
    * The input address buffer is expected to be in host byte order format.
    */
-  static Address Deserialize (const uint8_t* buf);
+  void Deserialize (TagBuffer buffer);
 
 private:
   friend bool operator == (const Address &a, const Address &b);
--- a/src/node/socket.cc	Wed May 28 11:28:06 2008 -0700
+++ b/src/node/socket.cc	Wed May 28 13:03:29 2008 -0700
@@ -304,27 +304,12 @@
 void
 SocketRxAddressTag::Serialize (TagBuffer i) const
 {
-  uint8_t len = m_address.GetSerializedSize ();
-  uint8_t* buffer = new uint8_t[len];
-  memset (buffer, 0, len);
-  m_address.Serialize (buffer, len);
-  i.Write (buffer, len);
-  delete [] buffer;
+  m_address.Serialize (i);
 }
 void
 SocketRxAddressTag::Deserialize (TagBuffer i)
 {
-  uint8_t type = i.ReadU8 (); 
-  uint8_t len = i.ReadU8 (); 
-  // Len is the length of the address starting from buffer[2]
-  NS_ASSERT (len >= 2);
-  uint8_t* buffer = new uint8_t[len];
-  memset (buffer, 0, len);
-  buffer[0] = type;
-  buffer[1] = len;
-  i.Read (buffer+2, len); // ReadU8 consumes a byte
-  m_address = Address::Deserialize (buffer);
-  delete [] buffer;
+  m_address.Deserialize (i);
 }
 
 SocketIpTtlTag::SocketIpTtlTag ()