Fix off-by-2 errors

This commit is contained in:
Mathieu Lacage
2008-05-28 13:03:29 -07:00
parent 27e5640201
commit 76dd4d1db4
3 changed files with 15 additions and 30 deletions

View File

@@ -114,21 +114,20 @@ Address::GetSerializedSize (void) const
}
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);

View File

@@ -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 @@ public:
* \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);

View File

@@ -304,27 +304,12 @@ SocketRxAddressTag::GetSerializedSize (void) const
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 ()