Skip to content

Commit 6e3da20

Browse files
authored
Merge pull request #3751 from softins/fix-issue-3747-crash
Add bounds checking before indexing vecvecTempMemory
2 parents 193258b + d545dbc commit 6e3da20

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
@@ -64,11 +64,13 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks, const boo
6464
// extract all data from buffer in temporary storage
6565
CVector<CVector<uint8_t>> vecvecTempMemory = vecvecMemory; // allocate worst case memory by copying
6666

67+
int iTempSize = vecvecTempMemory.size(); // for bounds checking
68+
6769
if ( !bNUseSequenceNumber )
6870
{
6971
int iPreviousDataCnt = 0;
7072

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

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

97-
while ( iBlockGetPos < iNumBlocksMemory )
100+
while ( iBlockGetPos < iNumBlocksMemory && iCurBlockPos < iTempSize )
98101
{
99102
veciTempBlockValid[iCurBlockPos] = veciBlockValid[iBlockGetPos];
100103
vecvecTempMemory[iCurBlockPos++] = vecvecMemory[iBlockGetPos++];
101104
}
102105

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

116-
for ( int iCurPos = 0; iCurPos < std::min ( iNewNumBlocks, iOldNumBlocksMemory ); iCurPos++ )
119+
for ( int iCurPos = 0; iCurPos < std::min ( iNewNumBlocks, iOldNumBlocksMemory ) && iCurPos < iTempSize; iCurPos++ )
117120
{
118121
veciBlockValid[iCurPos] = veciTempBlockValid[iCurPos];
119122
vecvecMemory[iCurPos] = vecvecTempMemory[iCurPos];

0 commit comments

Comments
 (0)