@@ -206,9 +206,6 @@ private static (byte[] Buffer, int Position) DecodeImpl(
206206 break ;
207207 }
208208
209- if ( readerBuffer . Length > MaxReadBufferSize )
210- throw new InvalidDataException ( "JSON token exceeds maximum buffer size." ) ;
211-
212209 var jsonReader = new Utf8JsonReader ( readerBuffer , isCompleted , readerState ) ;
213210
214211 var status = DecodeCore (
@@ -222,11 +219,16 @@ private static (byte[] Buffer, int Position) DecodeImpl(
222219
223220 switch ( status )
224221 {
225- case Lite3Core . Status . NeedsMoreData :
226- if ( jsonReader . BytesConsumed == 0 )
227- targetReadSize = targetReadSize >= MaxReadBufferSize / 2
228- ? MaxReadBufferSize
229- : targetReadSize * 2 ;
222+ case Lite3Core . Status . NeedsMoreData when ! isCompleted :
223+ if ( jsonReader . BytesConsumed > 0 )
224+ break ;
225+
226+ if ( targetReadSize >= MaxReadBufferSize )
227+ throw new InvalidDataException ( "Input requires buffering more than the maximum allowed size." ) ;
228+
229+ targetReadSize = targetReadSize >= MaxReadBufferSize / 2
230+ ? MaxReadBufferSize
231+ : targetReadSize * 2 ;
230232 break ;
231233
232234 case < 0 :
@@ -263,8 +265,6 @@ private static (byte[] Buffer, int Position) DecodeImpl(
263265 finally
264266 {
265267 ArrayPool < Frame > . Shared . Return ( frames ) ;
266-
267- await pipeReader . CompleteAsync ( ) . ConfigureAwait ( false ) ;
268268 }
269269
270270 return ( buffer , position ) ;
@@ -330,7 +330,7 @@ private static Lite3Core.Status DecodeDocument(
330330 Lite3Core . Status status ;
331331
332332 if ( ! reader . Read ( ) )
333- return reader . IsFinalBlock ? Lite3Core . Status . InsufficientBuffer : Lite3Core . Status . NeedsMoreData ;
333+ return Lite3Core . Status . NeedsMoreData ;
334334
335335 switch ( reader . TokenType )
336336 {
@@ -377,7 +377,7 @@ private static Lite3Core.Status DecodeObject(
377377 return stack . Push ( Frame . ForObjectSwitch ( offset , GetKeyRef ( arrayPool , ref reader ) ) ) ;
378378 }
379379
380- return reader . IsFinalBlock ? Lite3Core . Status . InsufficientBuffer : Lite3Core . Status . NeedsMoreData ;
380+ return Lite3Core . Status . NeedsMoreData ;
381381 }
382382
383383 private static Lite3Core . Status DecodeObjectSwitch (
@@ -485,7 +485,7 @@ private static Lite3Core.Status DecodeArray(
485485 return stack . Push ( Frame . ForArraySwitch ( offset ) ) ;
486486 }
487487
488- return reader . IsFinalBlock ? Lite3Core . Status . InsufficientBuffer : Lite3Core . Status . NeedsMoreData ;
488+ return Lite3Core . Status . NeedsMoreData ;
489489 }
490490
491491 private static Lite3Core . Status DecodeArraySwitch (
@@ -626,7 +626,7 @@ private struct FrameStack(Frame[] frames)
626626 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
627627 public Lite3Core . Status Push ( in Frame frame )
628628 {
629- if ( _index > _frames . Length - 1 ) return Lite3Core . Status . JsonNestingDepthExceededMax ;
629+ if ( _index >= _frames . Length - 1 ) return Lite3Core . Status . JsonNestingDepthExceededMax ;
630630 _frames [ ++ _index ] = frame ;
631631 return 0 ;
632632 }
0 commit comments