From 00409245b069ea5ad321875e90ced3a776b705b1 Mon Sep 17 00:00:00 2001 From: Maksim Golev Date: Sat, 29 Jul 2023 00:26:44 +0300 Subject: [PATCH] fix(#27769): Fix regression of "System.Text.Json". --- .../Json/Reader/Utf8JsonReader.MultiSegment.cs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs index 8635031292e32c..e7f4caa790e1f9 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs @@ -68,28 +68,17 @@ public Utf8JsonReader(ReadOnlySequence 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 memory, advance: true)) + if (jsonData.TryGet(ref _currentPosition, out ReadOnlyMemory 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));