@@ -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