Skip to content

Commit

Permalink
fix(dotnet#27769): Fix regression of "System.Text.Json".
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximys committed Jul 28, 2023
1 parent ff8699f commit 0040924
Showing 1 changed file with 3 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,17 @@ public Utf8JsonReader(ReadOnlySequence<byte> jsonData, bool isFinalBlock, JsonRe
bool firstSegmentIsEmpty = _buffer.Length == 0;
if (firstSegmentIsEmpty)
{
// Once we find a non-empty segment, we need to set current position to it.
// Therefore, track the next position in a copy before it gets advanced to the next segment.
SequencePosition previousNextPosition = _nextPosition;
while (jsonData.TryGet(ref _nextPosition, out ReadOnlyMemory<byte> memory, advance: true))
if (jsonData.TryGet(ref _currentPosition, out ReadOnlyMemory<byte> memory, advance: true))
{
// _currentPosition should point to the segment right befor the segment that _nextPosition points to.
_currentPosition = previousNextPosition;
if (memory.Length != 0)
{
_buffer = memory.Span;
break;
}
previousNextPosition = _nextPosition;
}
_nextPosition = _currentPosition;
}

// If firstSegmentIsEmpty is true,
// only check if we have reached the last segment but do not advance _nextPosition. The while loop above already advanced it.
// Otherwise, we would end up skipping a segment (i.e. advance = false).
// If firstSegmentIsEmpty is false,
// make sure to advance _nextPosition so that it is no longer the same as _currentPosition (i.e. advance = true).
_isLastSegment = !jsonData.TryGet(ref _nextPosition, out _, advance: !firstSegmentIsEmpty) && isFinalBlock; // Don't re-order to avoid short-circuiting
_isLastSegment = !jsonData.TryGet(ref _nextPosition, out _, advance: true) && isFinalBlock; // Don't re-order to avoid short-circuiting

Debug.Assert(!_nextPosition.Equals(_currentPosition));

Expand Down

0 comments on commit 0040924

Please sign in to comment.