@@ -323,7 +323,7 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
323
323
return ;
324
324
}
325
325
326
- // Assigned this before calculating the chunk size since that can throw
326
+ // Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedPrefix.
327
327
examined = reader . Position ;
328
328
329
329
var chunkSize = CalculateChunkSize ( ch1 , 0 ) ;
@@ -348,10 +348,12 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
348
348
return ;
349
349
}
350
350
351
+ // Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedPrefix.
352
+ examined = reader . Position ;
353
+
351
354
if ( ch1 == '\r ' && ch2 == '\n ' )
352
355
{
353
356
consumed = reader . Position ;
354
- examined = reader . Position ;
355
357
356
358
AddAndCheckConsumedBytes ( reader . Consumed ) ;
357
359
_inputLength = chunkSize ;
@@ -363,9 +365,6 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
363
365
ch1 = ch2 ;
364
366
}
365
367
366
- // Set examined so that we capture the progress that way made
367
- examined = reader . Position ;
368
-
369
368
// At this point, 10 bytes have been consumed which is enough to parse the max value "7FFFFFFF\r\n".
370
369
BadHttpRequestException . Throw ( RequestRejectionReason . BadChunkSizeData ) ;
371
370
}
@@ -453,10 +452,13 @@ private void ParseChunkedSuffix(ReadOnlySequence<byte> buffer, out SequencePosit
453
452
454
453
var suffixBuffer = buffer . Slice ( 0 , 2 ) ;
455
454
var suffixSpan = suffixBuffer . ToSpan ( ) ;
455
+
456
+ // Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedSuffix.
457
+ examined = suffixBuffer . End ;
458
+
456
459
if ( suffixSpan [ 0 ] == '\r ' && suffixSpan [ 1 ] == '\n ' )
457
460
{
458
461
consumed = suffixBuffer . End ;
459
- examined = suffixBuffer . End ;
460
462
AddAndCheckConsumedBytes ( 2 ) ;
461
463
_mode = Mode . Prefix ;
462
464
}
@@ -480,10 +482,12 @@ private void ParseChunkedTrailer(ReadOnlySequence<byte> buffer, out SequencePosi
480
482
var trailerBuffer = buffer . Slice ( 0 , 2 ) ;
481
483
var trailerSpan = trailerBuffer . ToSpan ( ) ;
482
484
485
+ // Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedTrailer.
486
+ examined = trailerBuffer . End ;
487
+
483
488
if ( trailerSpan [ 0 ] == '\r ' && trailerSpan [ 1 ] == '\n ' )
484
489
{
485
490
consumed = trailerBuffer . End ;
486
- examined = trailerBuffer . End ;
487
491
AddAndCheckConsumedBytes ( 2 ) ;
488
492
_mode = Mode . Complete ;
489
493
}
0 commit comments