Skip to content

Commit 902985f

Browse files
...
1 parent 0c31a8e commit 902985f

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

Lite3DotNet.SystemTextJson/Lite3JsonDecoder.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Lite3DotNet/Lite3Core.Status.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public enum Status
2929
ExpectedJsonValue,
3030
InsufficientBuffer,
3131
NeedsMoreData,
32-
BufferOverflow,
3332

3433
None = 0,
3534
IteratorDone = 1,
@@ -63,8 +62,7 @@ public enum Status
6362
Status.ExpectedJsonArrayOrObject => new InvalidOperationException("Expected JSON array or object."),
6463
Status.ExpectedJsonValue => new InvalidOperationException("Expected JSON value."),
6564
Status.JsonNestingDepthExceededMax => new InvalidOperationException("JSON nesting depth exceeded max."),
66-
Status.NeedsMoreData => new InvalidOperationException("Needs more data."),
67-
Status.BufferOverflow => new InvalidOperationException("Buffer overflow."),
65+
Status.NeedsMoreData => new InvalidOperationException("Unexpected end of input."),
6866

6967
_ => new InvalidOperationException($"An unknown error occurred ({status}).")
7068
};

0 commit comments

Comments
 (0)