@@ -137,6 +137,7 @@ public void DecodePixelRow(Span<byte> indices)
137
137
ref int suffixRef = ref MemoryMarshal . GetReference ( this . suffix . GetSpan ( ) ) ;
138
138
ref int pixelStackRef = ref MemoryMarshal . GetReference ( this . pixelStack . GetSpan ( ) ) ;
139
139
Span < byte > buffer = this . scratchBuffer . GetSpan ( ) ;
140
+ int maxTop = this . pixelStack . Length ( ) - 1 ;
140
141
141
142
int x = 0 ;
142
143
int xyz = 0 ;
@@ -204,7 +205,7 @@ public void DecodePixelRow(Span<byte> indices)
204
205
this . code = this . oldCode ;
205
206
}
206
207
207
- while ( this . code > this . clearCode )
208
+ while ( this . code > this . clearCode && this . top < maxTop )
208
209
{
209
210
Unsafe . Add ( ref pixelStackRef , ( uint ) this . top ++ ) = Unsafe. Add ( ref suffixRef , ( uint ) this . code ) ;
210
211
this . code = Unsafe . Add ( ref prefixRef , ( uint ) this . code ) ;
@@ -250,6 +251,7 @@ public void SkipIndices(int length)
250
251
ref int suffixRef = ref MemoryMarshal . GetReference ( this . suffix . GetSpan ( ) ) ;
251
252
ref int pixelStackRef = ref MemoryMarshal . GetReference ( this . pixelStack . GetSpan ( ) ) ;
252
253
Span < byte > buffer = this . scratchBuffer . GetSpan ( ) ;
254
+ int maxTop = this . pixelStack . Length ( ) - 1 ;
253
255
254
256
int xyz = 0 ;
255
257
while ( xyz < length )
@@ -316,7 +318,7 @@ public void SkipIndices(int length)
316
318
this . code = this . oldCode ;
317
319
}
318
320
319
- while ( this . code > this . clearCode )
321
+ while ( this . code > this . clearCode && this . top < maxTop )
320
322
{
321
323
Unsafe . Add ( ref pixelStackRef , ( uint ) this . top ++ ) = Unsafe. Add ( ref suffixRef , ( uint ) this . code ) ;
322
324
this . code = Unsafe . Add ( ref prefixRef , ( uint ) this . code ) ;
0 commit comments