diff --git a/src/common/buffer.cc b/src/common/buffer.cc index b764bd827..c36abdaba 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -1017,7 +1017,8 @@ Buffer::Iterator::Write (Iterator start, Iterator end) NS_ASSERT (m_data != start.m_data); uint32_t size = end.m_current - start.m_current; Iterator cur = start; - NS_ASSERT (CheckNoZero (m_current, m_current + size)); + NS_ASSERT_MSG (CheckNoZero (m_current, m_current + size), + GetWriteErrorMessage ()); if (start.m_current <= start.m_zeroStart) { uint32_t toCopy = std::min (size, start.m_zeroStart - start.m_current); @@ -1120,7 +1121,8 @@ Buffer::Iterator::WriteHtonU64 (uint64_t data) void Buffer::Iterator::Write (uint8_t const*buffer, uint32_t size) { - NS_ASSERT (CheckNoZero (m_current, size)); + NS_ASSERT_MSG (CheckNoZero (m_current, size), + GetWriteErrorMessage ()); uint8_t *to; if (m_current <= m_zeroStart) { @@ -1326,6 +1328,48 @@ Buffer::Iterator::GetSize (void) const return m_dataEnd - m_dataStart; } + +std::string +Buffer::Iterator::GetReadErrorMessage (void) const +{ + std::string str = "You have attempted to read beyond the bounds of the " + "available buffer space. This usually indicates that a " + "Header::Deserialize or Trailer::Deserialize method " + "is trying to read data which was not written by " + "a Header::Serialize or Trailer::Serialize method. " + "In short: check the code of your Serialize and Deserialize " + "methods."; + return str; +} +std::string +Buffer::Iterator::GetWriteErrorMessage (void) const +{ + std::string str; + if (m_current < m_dataStart) + { + str = "You have attempted to write before the start of the available " + "buffer space. This usually indicates that Trailer::GetSerializedSize " + "returned a size which is too small compared to what Trailer::Serialize " + "is actually using."; + } + else if (m_current >= m_dataEnd) + { + str = "You have attempted to write after the end of the available " + "buffer space. This usually indicates that Header::GetSerializedSize " + "returned a size which is too small compared to what Header::Serialize " + "is actually using."; + } + else + { + NS_ASSERT (m_current >= m_zeroStart && m_current < m_zeroEnd); + str = "You have attempted to write inside the payload area of the " + "buffer. This usually indicates that your Serialize method uses more " + "buffer space than what your GetSerialized method returned."; + } + return str; +} + + //----------------------------------------------------------------------------- // Unit tests //----------------------------------------------------------------------------- diff --git a/src/common/buffer.h b/src/common/buffer.h index fe74d8d07..6a0472ddd 100644 --- a/src/common/buffer.h +++ b/src/common/buffer.h @@ -365,6 +365,8 @@ public: bool Check (uint32_t i) const; uint16_t SlowReadNtohU16 (void); uint32_t SlowReadNtohU32 (void); + std::string GetReadErrorMessage (void) const; + std::string GetWriteErrorMessage (void) const; /* offset in virtual bytes from the start of the data buffer to the * start of the "virtual zero area". @@ -579,7 +581,8 @@ namespace ns3 { void Buffer::Iterator::WriteU8 (uint8_t data) { - NS_ASSERT (Check (m_current)); + NS_ASSERT_MSG (Check (m_current), + GetWriteErrorMessage ()); if (m_current < m_zeroStart) { @@ -596,7 +599,8 @@ Buffer::Iterator::WriteU8 (uint8_t data) void Buffer::Iterator::WriteU8 (uint8_t data, uint32_t len) { - NS_ASSERT (CheckNoZero (m_current, m_current + len)); + NS_ASSERT_MSG (CheckNoZero (m_current, m_current + len), + GetWriteErrorMessage ()); if (m_current <= m_zeroStart) { memset (&(m_data[m_current]), data, len); @@ -613,7 +617,8 @@ Buffer::Iterator::WriteU8 (uint8_t data, uint32_t len) void Buffer::Iterator::WriteHtonU16 (uint16_t data) { - NS_ASSERT (CheckNoZero (m_current, m_current + 2)); + NS_ASSERT_MSG (CheckNoZero (m_current, m_current + 2), + GetWriteErrorMessage ()); uint8_t *buffer; if (m_current + 2 <= m_zeroStart) { @@ -631,7 +636,9 @@ Buffer::Iterator::WriteHtonU16 (uint16_t data) void Buffer::Iterator::WriteHtonU32 (uint32_t data) { - NS_ASSERT (CheckNoZero (m_current, m_current + 4)); + NS_ASSERT_MSG (CheckNoZero (m_current, m_current + 4), + GetWriteErrorMessage ()); + uint8_t *buffer; if (m_current + 4 <= m_zeroStart) { @@ -703,8 +710,9 @@ Buffer::Iterator::ReadNtohU32 (void) uint8_t Buffer::Iterator::ReadU8 (void) { - NS_ASSERT (m_current >= m_dataStart && - m_current <= m_dataEnd); + NS_ASSERT_MSG (m_current >= m_dataStart && + m_current <= m_dataEnd, + GetReadErrorMessage ()); if (m_current < m_zeroStart) {