From: Krzysztof Wicher Date: Thu, 27 Jun 2019 17:14:05 +0000 (-0700) Subject: Fix BytesConsumed when skipping single line comments (multi-segment) (dotnet/corefx... X-Git-Tag: submit/tizen/20210909.063632~11031^2~1103 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e08560d4fc7a701b9463c1230e680a2beb2aaa6d;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Fix BytesConsumed when skipping single line comments (multi-segment) (dotnet/corefx#38965) * Fix BytesConsumed when skipping single line comments (multi-segment) * rename ret -> skipSucceeded Commit migrated from https://github.com/dotnet/corefx/commit/32f061ccb2d2b6a804f8040717f448d97cbf862d --- 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 dbb057b..ab72151 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 @@ -438,7 +438,7 @@ namespace System.Text.Json if (marker == JsonConstants.Slash) { SequencePosition copy = _currentPosition; - if (!ConsumeCommentMultiSegment()) + if (!SkipOrConsumeCommentMultiSegmentWithRollback()) { _currentPosition = copy; return false; @@ -1603,7 +1603,7 @@ namespace System.Text.Json { if (marker == JsonConstants.Slash) { - return ConsumeCommentMultiSegment() ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBackState; + return SkipOrConsumeCommentMultiSegmentWithRollback() ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBackState; } if (_tokenType == JsonTokenType.Comment) { @@ -1665,7 +1665,7 @@ namespace System.Text.Json if (_readerOptions.CommentHandling == JsonCommentHandling.Allow && first == JsonConstants.Slash) { _trailingCommaBeforeComment = true; - return ConsumeCommentMultiSegment() ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBackState; + return SkipOrConsumeCommentMultiSegmentWithRollback() ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBackState; } if (_inObject) @@ -1806,7 +1806,7 @@ namespace System.Text.Json if (first == JsonConstants.Slash) { _trailingCommaBeforeComment = true; - if (ConsumeCommentMultiSegment()) + if (SkipOrConsumeCommentMultiSegmentWithRollback()) { goto Done; } @@ -1951,7 +1951,7 @@ namespace System.Text.Json { while (marker == JsonConstants.Slash) { - if (SkipCommentMultiSegment(out _)) + if (SkipOrConsumeCommentMultiSegmentWithRollback()) { if (!HasMoreDataMultiSegment()) { @@ -1986,7 +1986,7 @@ namespace System.Text.Json { while (marker == JsonConstants.Slash) { - if (SkipCommentMultiSegment(out _)) + if (SkipOrConsumeCommentMultiSegmentWithRollback()) { // The next character must be a start of a property name or value. if (!HasMoreDataMultiSegment(resource)) @@ -2185,35 +2185,42 @@ namespace System.Text.Json return ConsumeTokenResult.NotEnoughDataRollBackState; } - private bool ConsumeCommentMultiSegment() + private bool SkipOrConsumeCommentMultiSegmentWithRollback() { long prevTotalConsumed = BytesConsumed; SequencePosition start = new SequencePosition(_currentPosition.GetObject(), _currentPosition.GetInteger() + _consumed); - bool ret = SkipCommentMultiSegment(out int tailBytesToIgnore); + bool skipSucceeded = SkipCommentMultiSegment(out int tailBytesToIgnore); - if (ret) + if (skipSucceeded) { - SequencePosition end = new SequencePosition(_currentPosition.GetObject(), _currentPosition.GetInteger() + _consumed); + Debug.Assert( + _readerOptions.CommentHandling == JsonCommentHandling.Allow || + _readerOptions.CommentHandling == JsonCommentHandling.Skip); - ReadOnlySequence commentSequence = _sequence.Slice(start, end); - commentSequence = commentSequence.Slice(2, commentSequence.Length - 2 - tailBytesToIgnore); - HasValueSequence = !commentSequence.IsSingleSegment; - - if (HasValueSequence) - { - ValueSequence = commentSequence; - } - else + if (_readerOptions.CommentHandling == JsonCommentHandling.Allow) { - ValueSpan = commentSequence.First.Span; - } + SequencePosition end = new SequencePosition(_currentPosition.GetObject(), _currentPosition.GetInteger() + _consumed); - if (_tokenType != JsonTokenType.Comment) - { - _previousTokenType = _tokenType; - } + ReadOnlySequence commentSequence = _sequence.Slice(start, end); + commentSequence = commentSequence.Slice(2, commentSequence.Length - 2 - tailBytesToIgnore); + HasValueSequence = !commentSequence.IsSingleSegment; - _tokenType = JsonTokenType.Comment; + if (HasValueSequence) + { + ValueSequence = commentSequence; + } + else + { + ValueSpan = commentSequence.First.Span; + } + + if (_tokenType != JsonTokenType.Comment) + { + _previousTokenType = _tokenType; + } + + _tokenType = JsonTokenType.Comment; + } } else { @@ -2228,7 +2235,7 @@ namespace System.Text.Json _consumed = 0; } - return ret; + return skipSucceeded; } private bool SkipCommentMultiSegment(out int tailBytesToIgnore) diff --git a/src/libraries/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs b/src/libraries/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs index 70e1f99..567eb8f 100644 --- a/src/libraries/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs +++ b/src/libraries/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs @@ -911,7 +911,6 @@ namespace System.Text.Json.Tests } } - [ActiveIssue(38927)] [Theory] [MemberData(nameof(CommentTestLineSeparators))] public static void SkipSingleLineCommentMultiSpanTest(string lineSeparator)