Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
// extract all data from buffer in temporary storage
CVector<CVector<uint8_t>> vecvecTempMemory = vecvecMemory; // allocate worst case memory by copying

int iTempSize = vecvecTempMemory.size(); // for bounds checking

if ( !bNUseSequenceNumber )
{
int iPreviousDataCnt = 0;

while ( Get ( vecvecTempMemory[iPreviousDataCnt], iBlockSize ) )
while ( iPreviousDataCnt < iTempSize && Get ( vecvecTempMemory[iPreviousDataCnt], iBlockSize ) )
{
iPreviousDataCnt++;
}
Expand All @@ -80,6 +82,7 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
// data back as the new buffer size can hold)
int iDataCnt = 0;

// iPreviousDataCnt will be at most iTempSize, so an additional check on iDataCnt is not needed
while ( ( iDataCnt < iPreviousDataCnt ) && Put ( vecvecTempMemory[iDataCnt], iBlockSize ) )
{
iDataCnt++;
Expand All @@ -94,13 +97,13 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
const int iOldBlockGetPos = iBlockGetPos;
int iCurBlockPos = 0;

while ( iBlockGetPos < iNumBlocksMemory )
while ( iBlockGetPos < iNumBlocksMemory && iCurBlockPos < iTempSize )
{
veciTempBlockValid[iCurBlockPos] = veciBlockValid[iBlockGetPos];
vecvecTempMemory[iCurBlockPos++] = vecvecMemory[iBlockGetPos++];
}

for ( iBlockGetPos = 0; iBlockGetPos < iOldBlockGetPos; iBlockGetPos++ )
for ( iBlockGetPos = 0; iBlockGetPos < iOldBlockGetPos && iCurBlockPos < iTempSize; iBlockGetPos++ )
{
veciTempBlockValid[iCurBlockPos] = veciBlockValid[iBlockGetPos];
vecvecTempMemory[iCurBlockPos++] = vecvecMemory[iBlockGetPos];
Expand All @@ -113,7 +116,7 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
iSequenceNumberAtGetPos = iOldSequenceNumberAtGetPos;
iBlockGetPos = 0; // per definition

for ( int iCurPos = 0; iCurPos < std::min ( iNewNumBlocks, iOldNumBlocksMemory ); iCurPos++ )
for ( int iCurPos = 0; iCurPos < std::min ( iNewNumBlocks, iOldNumBlocksMemory ) && iCurPos < iTempSize; iCurPos++ )
{
veciBlockValid[iCurPos] = veciTempBlockValid[iCurPos];
vecvecMemory[iCurPos] = vecvecTempMemory[iCurPos];
Expand Down
Loading