1
0

Added tons more asserts to bytebuffer

This commit is contained in:
Tycho 2014-03-08 11:18:51 -08:00
parent 27fa2b72ba
commit a6ed75c1fb

View File

@ -185,6 +185,7 @@ bool cByteBuffer::Write(const char * a_Bytes, size_t a_Count)
{ {
return false; return false;
} }
ASSERT(m_BufferSize >= m_WritePos);
size_t TillEnd = m_BufferSize - m_WritePos; size_t TillEnd = m_BufferSize - m_WritePos;
if (TillEnd <= a_Count) if (TillEnd <= a_Count)
{ {
@ -223,9 +224,13 @@ size_t cByteBuffer::GetFreeSpace(void) const
if (m_WritePos >= m_DataStart) if (m_WritePos >= m_DataStart)
{ {
// Wrap around the buffer end: // Wrap around the buffer end:
ASSERT(m_BufferSize >= m_WritePos);
ASSERT((m_BufferSize - m_WritePos + m_DataStart) >= 1);
return m_BufferSize - m_WritePos + m_DataStart - 1; return m_BufferSize - m_WritePos + m_DataStart - 1;
} }
// Single free space partition: // Single free space partition:
ASSERT(m_BufferSize >= m_WritePos);
ASSERT(m_BufferSize - m_WritePos >= 1);
return m_DataStart - m_WritePos - 1; return m_DataStart - m_WritePos - 1;
} }
@ -238,6 +243,8 @@ size_t cByteBuffer::GetUsedSpace(void) const
{ {
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
ASSERT(m_BufferSize >= GetFreeSpace());
ASSERT((m_BufferSize - GetFreeSpace()) >= 1);
return m_BufferSize - GetFreeSpace() - 1; return m_BufferSize - GetFreeSpace() - 1;
} }
@ -253,9 +260,11 @@ size_t cByteBuffer::GetReadableSpace(void) const
if (m_ReadPos > m_WritePos) if (m_ReadPos > m_WritePos)
{ {
// Wrap around the buffer end: // Wrap around the buffer end:
ASSERT(m_BufferSize >= m_ReadPos);
return m_BufferSize - m_ReadPos + m_WritePos; return m_BufferSize - m_ReadPos + m_WritePos;
} }
// Single readable space partition: // Single readable space partition:
ASSERT(m_WritePos >= m_ReadPos);
return m_WritePos - m_ReadPos ; return m_WritePos - m_ReadPos ;
} }
@ -654,11 +663,10 @@ bool cByteBuffer::ReadBuf(void * a_Buffer, size_t a_Count)
{ {
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
ASSERT(a_Count >= 0);
NEEDBYTES(a_Count); NEEDBYTES(a_Count);
char * Dst = (char *)a_Buffer; // So that we can do byte math char * Dst = (char *)a_Buffer; // So that we can do byte math
ASSERT(m_BufferSize >= m_ReadPos);
size_t BytesToEndOfBuffer = m_BufferSize - m_ReadPos; size_t BytesToEndOfBuffer = m_BufferSize - m_ReadPos;
ASSERT(BytesToEndOfBuffer >= 0); // Sanity check
if (BytesToEndOfBuffer <= a_Count) if (BytesToEndOfBuffer <= a_Count)
{ {
// Reading across the ringbuffer end, read the first part and adjust parameters: // Reading across the ringbuffer end, read the first part and adjust parameters:
@ -688,9 +696,9 @@ bool cByteBuffer::WriteBuf(const void * a_Buffer, size_t a_Count)
{ {
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
ASSERT(a_Count >= 0);
PUTBYTES(a_Count); PUTBYTES(a_Count);
char * Src = (char *)a_Buffer; // So that we can do byte math char * Src = (char *)a_Buffer; // So that we can do byte math
ASSERT(m_BufferSize >= m_ReadPos);
size_t BytesToEndOfBuffer = m_BufferSize - m_WritePos; size_t BytesToEndOfBuffer = m_BufferSize - m_WritePos;
if (BytesToEndOfBuffer <= a_Count) if (BytesToEndOfBuffer <= a_Count)
{ {
@ -718,18 +726,18 @@ bool cByteBuffer::ReadString(AString & a_String, size_t a_Count)
{ {
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
ASSERT(a_Count >= 0);
NEEDBYTES(a_Count); NEEDBYTES(a_Count);
a_String.clear(); a_String.clear();
a_String.reserve(a_Count); a_String.reserve(a_Count);
ASSERT(m_BufferSize >= m_ReadPos);
size_t BytesToEndOfBuffer = m_BufferSize - m_ReadPos; size_t BytesToEndOfBuffer = m_BufferSize - m_ReadPos;
ASSERT(BytesToEndOfBuffer >= 0); // Sanity check
if (BytesToEndOfBuffer <= a_Count) if (BytesToEndOfBuffer <= a_Count)
{ {
// Reading across the ringbuffer end, read the first part and adjust parameters: // Reading across the ringbuffer end, read the first part and adjust parameters:
if (BytesToEndOfBuffer > 0) if (BytesToEndOfBuffer > 0)
{ {
a_String.assign(m_Buffer + m_ReadPos, BytesToEndOfBuffer); a_String.assign(m_Buffer + m_ReadPos, BytesToEndOfBuffer);
ASSERT(a_Count >= BytesToEndOfBuffer);
a_Count -= BytesToEndOfBuffer; a_Count -= BytesToEndOfBuffer;
} }
m_ReadPos = 0; m_ReadPos = 0;
@ -771,7 +779,6 @@ bool cByteBuffer::SkipRead(size_t a_Count)
{ {
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
ASSERT(a_Count >= 0);
if (!CanReadBytes(a_Count)) if (!CanReadBytes(a_Count))
{ {
return false; return false;
@ -809,6 +816,7 @@ bool cByteBuffer::ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes)
size_t num = (a_NumBytes > sizeof(buf)) ? sizeof(buf) : a_NumBytes; size_t num = (a_NumBytes > sizeof(buf)) ? sizeof(buf) : a_NumBytes;
VERIFY(ReadBuf(buf, num)); VERIFY(ReadBuf(buf, num));
VERIFY(a_Dst.Write(buf, num)); VERIFY(a_Dst.Write(buf, num));
ASSERT(a_NumBytes >= num);
a_NumBytes -= num; a_NumBytes -= num;
} }
return true; return true;
@ -846,13 +854,15 @@ void cByteBuffer::ReadAgain(AString & a_Out)
// Used by ProtoProxy to repeat communication twice, once for parsing and the other time for the remote party // Used by ProtoProxy to repeat communication twice, once for parsing and the other time for the remote party
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
int DataStart = m_DataStart; size_t DataStart = m_DataStart;
if (m_ReadPos < m_DataStart) if (m_ReadPos < m_DataStart)
{ {
// Across the ringbuffer end, read the first part and adjust next part's start: // Across the ringbuffer end, read the first part and adjust next part's start:
ASSERT(m_BufferSize >= m_DataStart);
a_Out.append(m_Buffer + m_DataStart, m_BufferSize - m_DataStart); a_Out.append(m_Buffer + m_DataStart, m_BufferSize - m_DataStart);
DataStart = 0; DataStart = 0;
} }
ASSERT(m_ReadPos >= DataStart);
a_Out.append(m_Buffer + DataStart, m_ReadPos - DataStart); a_Out.append(m_Buffer + DataStart, m_ReadPos - DataStart);
} }