summaryrefslogtreecommitdiffstats
path: root/src/ByteBuffer.cpp
diff options
context:
space:
mode:
authorTycho <work.tycho+git@gmail.com>2014-03-08 20:18:51 +0100
committerTycho <work.tycho+git@gmail.com>2014-03-08 20:18:51 +0100
commita6ed75c1fbd7d3a38336e8649b59950837529b14 (patch)
tree7e9698d2cd88af1c2c0c26e0a8ed20b691ca8a1c /src/ByteBuffer.cpp
parentEnabled self test of bytebuffer (diff)
downloadcuberite-a6ed75c1fbd7d3a38336e8649b59950837529b14.tar
cuberite-a6ed75c1fbd7d3a38336e8649b59950837529b14.tar.gz
cuberite-a6ed75c1fbd7d3a38336e8649b59950837529b14.tar.bz2
cuberite-a6ed75c1fbd7d3a38336e8649b59950837529b14.tar.lz
cuberite-a6ed75c1fbd7d3a38336e8649b59950837529b14.tar.xz
cuberite-a6ed75c1fbd7d3a38336e8649b59950837529b14.tar.zst
cuberite-a6ed75c1fbd7d3a38336e8649b59950837529b14.zip
Diffstat (limited to '')
-rw-r--r--src/ByteBuffer.cpp24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/ByteBuffer.cpp b/src/ByteBuffer.cpp
index adb8e9294..67fe1012c 100644
--- a/src/ByteBuffer.cpp
+++ b/src/ByteBuffer.cpp
@@ -185,6 +185,7 @@ bool cByteBuffer::Write(const char * a_Bytes, size_t a_Count)
{
return false;
}
+ ASSERT(m_BufferSize >= m_WritePos);
size_t TillEnd = m_BufferSize - m_WritePos;
if (TillEnd <= a_Count)
{
@@ -223,9 +224,13 @@ size_t cByteBuffer::GetFreeSpace(void) const
if (m_WritePos >= m_DataStart)
{
// 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;
}
// Single free space partition:
+ ASSERT(m_BufferSize >= m_WritePos);
+ ASSERT(m_BufferSize - m_WritePos >= 1);
return m_DataStart - m_WritePos - 1;
}
@@ -238,6 +243,8 @@ size_t cByteBuffer::GetUsedSpace(void) const
{
CHECK_THREAD;
CheckValid();
+ ASSERT(m_BufferSize >= GetFreeSpace());
+ ASSERT((m_BufferSize - GetFreeSpace()) >= 1);
return m_BufferSize - GetFreeSpace() - 1;
}
@@ -253,9 +260,11 @@ size_t cByteBuffer::GetReadableSpace(void) const
if (m_ReadPos > m_WritePos)
{
// Wrap around the buffer end:
+ ASSERT(m_BufferSize >= m_ReadPos);
return m_BufferSize - m_ReadPos + m_WritePos;
}
// Single readable space partition:
+ ASSERT(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;
CheckValid();
- ASSERT(a_Count >= 0);
NEEDBYTES(a_Count);
char * Dst = (char *)a_Buffer; // So that we can do byte math
+ ASSERT(m_BufferSize >= m_ReadPos);
size_t BytesToEndOfBuffer = m_BufferSize - m_ReadPos;
- ASSERT(BytesToEndOfBuffer >= 0); // Sanity check
if (BytesToEndOfBuffer <= a_Count)
{
// 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;
CheckValid();
- ASSERT(a_Count >= 0);
PUTBYTES(a_Count);
char * Src = (char *)a_Buffer; // So that we can do byte math
+ ASSERT(m_BufferSize >= m_ReadPos);
size_t BytesToEndOfBuffer = m_BufferSize - m_WritePos;
if (BytesToEndOfBuffer <= a_Count)
{
@@ -718,18 +726,18 @@ bool cByteBuffer::ReadString(AString & a_String, size_t a_Count)
{
CHECK_THREAD;
CheckValid();
- ASSERT(a_Count >= 0);
NEEDBYTES(a_Count);
a_String.clear();
a_String.reserve(a_Count);
+ ASSERT(m_BufferSize >= m_ReadPos);
size_t BytesToEndOfBuffer = m_BufferSize - m_ReadPos;
- ASSERT(BytesToEndOfBuffer >= 0); // Sanity check
if (BytesToEndOfBuffer <= a_Count)
{
// Reading across the ringbuffer end, read the first part and adjust parameters:
if (BytesToEndOfBuffer > 0)
{
a_String.assign(m_Buffer + m_ReadPos, BytesToEndOfBuffer);
+ ASSERT(a_Count >= BytesToEndOfBuffer);
a_Count -= BytesToEndOfBuffer;
}
m_ReadPos = 0;
@@ -771,7 +779,6 @@ bool cByteBuffer::SkipRead(size_t a_Count)
{
CHECK_THREAD;
CheckValid();
- ASSERT(a_Count >= 0);
if (!CanReadBytes(a_Count))
{
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;
VERIFY(ReadBuf(buf, num));
VERIFY(a_Dst.Write(buf, num));
+ ASSERT(a_NumBytes >= num);
a_NumBytes -= num;
}
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
CHECK_THREAD;
CheckValid();
- int DataStart = m_DataStart;
+ size_t DataStart = m_DataStart;
if (m_ReadPos < m_DataStart)
{
// 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);
DataStart = 0;
}
+ ASSERT(m_ReadPos >= DataStart);
a_Out.append(m_Buffer + DataStart, m_ReadPos - DataStart);
}