Skip to content

Commit 5c5c0c8

Browse files
softinsann0see
authored andcommitted
Add bounds checking before indexing vecvecTempMemory
Fixes #3747
1 parent 6953c57 commit 5c5c0c8

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/buffer.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
4242
// extract all data from buffer in temporary storage
4343
CVector<CVector<uint8_t>> vecvecTempMemory = vecvecMemory; // allocate worst case memory by copying
4444

45+
int iTempSize = vecvecTempMemory.size(); // for bounds checking
46+
4547
if ( !bNUseSequenceNumber )
4648
{
4749
int iPreviousDataCnt = 0;
4850

49-
while ( Get ( vecvecTempMemory[iPreviousDataCnt], iBlockSize ) )
51+
while ( iPreviousDataCnt < iTempSize && Get ( vecvecTempMemory[iPreviousDataCnt], iBlockSize ) )
5052
{
5153
iPreviousDataCnt++;
5254
}
@@ -58,6 +60,7 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
5860
// data back as the new buffer size can hold)
5961
int iDataCnt = 0;
6062

63+
// iPreviousDataCnt will be at most iTempSize, so an additional check on iDataCnt is not needed
6164
while ( ( iDataCnt < iPreviousDataCnt ) && Put ( vecvecTempMemory[iDataCnt], iBlockSize ) )
6265
{
6366
iDataCnt++;
@@ -72,13 +75,13 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
7275
const int iOldBlockGetPos = iBlockGetPos;
7376
int iCurBlockPos = 0;
7477

75-
while ( iBlockGetPos < iNumBlocksMemory )
78+
while ( iBlockGetPos < iNumBlocksMemory && iCurBlockPos < iTempSize )
7679
{
7780
veciTempBlockValid[iCurBlockPos] = veciBlockValid[iBlockGetPos];
7881
vecvecTempMemory[iCurBlockPos++] = vecvecMemory[iBlockGetPos++];
7982
}
8083

81-
for ( iBlockGetPos = 0; iBlockGetPos < iOldBlockGetPos; iBlockGetPos++ )
84+
for ( iBlockGetPos = 0; iBlockGetPos < iOldBlockGetPos && iCurBlockPos < iTempSize; iBlockGetPos++ )
8285
{
8386
veciTempBlockValid[iCurBlockPos] = veciBlockValid[iBlockGetPos];
8487
vecvecTempMemory[iCurBlockPos++] = vecvecMemory[iBlockGetPos];
@@ -91,7 +94,7 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
9194
iSequenceNumberAtGetPos = iOldSequenceNumberAtGetPos;
9295
iBlockGetPos = 0; // per definition
9396

94-
for ( int iCurPos = 0; iCurPos < std::min ( iNewNumBlocks, iOldNumBlocksMemory ); iCurPos++ )
97+
for ( int iCurPos = 0; iCurPos < std::min ( iNewNumBlocks, iOldNumBlocksMemory ) && iCurPos < iTempSize; iCurPos++ )
9598
{
9699
veciBlockValid[iCurPos] = veciTempBlockValid[iCurPos];
97100
vecvecMemory[iCurPos] = vecvecTempMemory[iCurPos];

0 commit comments

Comments
 (0)