Skip to content

Commit c11d048

Browse files
committed
Fixed exception on slicing more that exists
1 parent ead9f87 commit c11d048

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/libraries/System.Text.Json/Common/JsonSeparatorNamingPolicy.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,25 @@ void WriteWord(ReadOnlySpan<char> word, ref Span<char> result)
5050
return;
5151
}
5252

53-
Span<char> destination = result.Slice(resultLength != 0
54-
? resultLength + 1
55-
: resultLength);
56-
5753
int written;
5854
while (true)
5955
{
60-
written = _lowercase
61-
? word.ToLowerInvariant(destination)
62-
: word.ToUpperInvariant(destination);
56+
var destinationOffset = resultLength != 0
57+
? resultLength + 1
58+
: resultLength;
6359

64-
if (written > 0)
60+
if (destinationOffset < result.Length)
6561
{
66-
break;
62+
Span<char> destination = result.Slice(destinationOffset);
63+
64+
written = _lowercase
65+
? word.ToLowerInvariant(destination)
66+
: word.ToUpperInvariant(destination);
67+
68+
if (written > 0)
69+
{
70+
break;
71+
}
6772
}
6873

6974
ExpandBuffer(ref result);

0 commit comments

Comments
 (0)