Skip to content

Commit 5c4d4fd

Browse files
CodeBlancheiriktsarpalis
authored andcommitted
Follow-up feedback from dotnet#54186.
1 parent 62fa1f8 commit 5c4d4fd

25 files changed

+89
-68
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ internal static class JsonConstants
9595
public const int MinimumDateTimeParseLength = 10; // YYYY-MM-DD
9696
public const int MaximumEscapedDateTimeOffsetParseLength = MaxExpansionFactorWhileEscaping * MaximumDateTimeOffsetParseLength;
9797

98+
public const int MaximumLiteralLength = 5; // Must be able to fit null, true, & false.
99+
98100
// Encoding Helpers
99101
public const char HighSurrogateStart = '\ud800';
100102
public const char HighSurrogateEnd = '\udbff';

src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Escaping.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static byte[] EscapeValue(
3838
int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);
3939

4040
Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ?
41-
stackalloc byte[length] :
41+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
4242
(valueArray = ArrayPool<byte>.Shared.Rent(length));
4343

4444
JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written);
@@ -66,7 +66,7 @@ private static byte[] GetEscapedPropertyNameSection(
6666
int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);
6767

6868
Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ?
69-
stackalloc byte[length] :
69+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
7070
(valueArray = ArrayPool<byte>.Shared.Rent(length));
7171

7272
JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static bool TryGetUnescapedBase64Bytes(ReadOnlySpan<byte> utf8Source, int
1515
byte[]? unescapedArray = null;
1616

1717
Span<byte> utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocThreshold ?
18-
stackalloc byte[utf8Source.Length] :
18+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, utf8Source.Length) :
1919
(unescapedArray = ArrayPool<byte>.Shared.Rent(utf8Source.Length));
2020

2121
Unescape(utf8Source, utf8Unescaped, idx, out int written);
@@ -45,7 +45,7 @@ public static string GetUnescapedString(ReadOnlySpan<byte> utf8Source, int idx)
4545
byte[]? pooledName = null;
4646

4747
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
48-
stackalloc byte[length] :
48+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
4949
(pooledName = ArrayPool<byte>.Shared.Rent(length));
5050

5151
Unescape(utf8Source, utf8Unescaped, idx, out int written);
@@ -72,7 +72,7 @@ public static ReadOnlySpan<byte> GetUnescapedSpan(ReadOnlySpan<byte> utf8Source,
7272
byte[]? pooledName = null;
7373

7474
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
75-
stackalloc byte[length] :
75+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
7676
(pooledName = ArrayPool<byte>.Shared.Rent(length));
7777

7878
Unescape(utf8Source, utf8Unescaped, idx, out int written);
@@ -97,7 +97,7 @@ public static bool UnescapeAndCompare(ReadOnlySpan<byte> utf8Source, ReadOnlySpa
9797
byte[]? unescapedArray = null;
9898

9999
Span<byte> utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocThreshold ?
100-
stackalloc byte[utf8Source.Length] :
100+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, utf8Source.Length) :
101101
(unescapedArray = ArrayPool<byte>.Shared.Rent(utf8Source.Length));
102102

103103
Unescape(utf8Source, utf8Unescaped, 0, out int written);
@@ -128,11 +128,11 @@ public static bool UnescapeAndCompare(ReadOnlySequence<byte> utf8Source, ReadOnl
128128
int length = checked((int)utf8Source.Length);
129129

130130
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
131-
stackalloc byte[length] :
131+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
132132
(unescapedArray = ArrayPool<byte>.Shared.Rent(length));
133133

134134
Span<byte> utf8Escaped = length <= JsonConstants.StackallocThreshold ?
135-
stackalloc byte[length] :
135+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
136136
(escapedArray = ArrayPool<byte>.Shared.Rent(length));
137137

138138
utf8Source.CopyTo(utf8Escaped);
@@ -175,7 +175,7 @@ public static bool TryDecodeBase64(ReadOnlySpan<byte> utf8Unescaped, [NotNullWhe
175175
byte[]? pooledArray = null;
176176

177177
Span<byte> byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocThreshold ?
178-
stackalloc byte[utf8Unescaped.Length] :
178+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, utf8Unescaped.Length) :
179179
(pooledArray = ArrayPool<byte>.Shared.Rent(utf8Unescaped.Length));
180180

181181
OperationStatus status = Base64.DecodeFromUtf8(utf8Unescaped, byteSpan, out int bytesConsumed, out int bytesWritten);

src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public static bool TryGetEscapedDateTime(ReadOnlySpan<byte> source, out DateTime
252252
Debug.Assert(backslash != -1);
253253

254254
Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength);
255-
Span<byte> sourceUnescaped = stackalloc byte[source.Length];
255+
Span<byte> sourceUnescaped = (stackalloc byte[JsonConstants.MaximumEscapedDateTimeOffsetParseLength]).Slice(0, source.Length);
256256

257257
Unescape(source, sourceUnescaped, backslash, out int written);
258258
Debug.Assert(written > 0);
@@ -277,7 +277,7 @@ public static bool TryGetEscapedDateTimeOffset(ReadOnlySpan<byte> source, out Da
277277
Debug.Assert(backslash != -1);
278278

279279
Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength);
280-
Span<byte> sourceUnescaped = stackalloc byte[source.Length];
280+
Span<byte> sourceUnescaped = (stackalloc byte[JsonConstants.MaximumEscapedDateTimeOffsetParseLength]).Slice(0, source.Length);
281281

282282
Unescape(source, sourceUnescaped, backslash, out int written);
283283
Debug.Assert(written > 0);
@@ -303,7 +303,7 @@ public static bool TryGetEscapedGuid(ReadOnlySpan<byte> source, out Guid value)
303303
int idx = source.IndexOf(JsonConstants.BackSlash);
304304
Debug.Assert(idx != -1);
305305

306-
Span<byte> utf8Unescaped = stackalloc byte[source.Length];
306+
Span<byte> utf8Unescaped = (stackalloc byte[JsonConstants.MaximumEscapedGuidLength]).Slice(0, source.Length);
307307

308308
Unescape(source, utf8Unescaped, idx, out int written);
309309
Debug.Assert(written > 0);

src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,9 @@ private bool ConsumeLiteralMultiSegment(ReadOnlySpan<byte> literal, JsonTokenTyp
540540

541541
private bool CheckLiteralMultiSegment(ReadOnlySpan<byte> span, ReadOnlySpan<byte> literal, out int consumed)
542542
{
543-
Debug.Assert(span.Length > 0 && span[0] == literal[0]);
543+
Debug.Assert(span.Length > 0 && span[0] == literal[0] && literal.Length <= JsonConstants.MaximumLiteralLength);
544544

545-
Span<byte> readSoFar = stackalloc byte[literal.Length];
545+
Span<byte> readSoFar = (stackalloc byte[JsonConstants.MaximumLiteralLength]).Slice(0, literal.Length);
546546
int written = 0;
547547

548548
long prevTotalConsumed = _totalConsumed;

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
5454
int backslash = source.IndexOf(JsonConstants.BackSlash);
5555
Debug.Assert(backslash != -1);
5656

57-
Span<byte> sourceUnescaped = stackalloc byte[source.Length];
57+
Span<byte> sourceUnescaped = (stackalloc byte[MaximumEscapedTimeSpanFormatLength]).Slice(0, source.Length);
5858

5959
JsonReaderHelper.Unescape(source, sourceUnescaped, backslash, out int written);
6060
Debug.Assert(written > 0);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Bytes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private void WriteBase64EscapeProperty(ReadOnlySpan<char> propertyName, ReadOnly
140140
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
141141

142142
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
143-
stackalloc char[length] :
143+
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
144144
(propertyArray = ArrayPool<char>.Shared.Rent(length));
145145

146146
JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
@@ -163,7 +163,7 @@ private void WriteBase64EscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Read
163163
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
164164

165165
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
166-
stackalloc byte[length] :
166+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
167167
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
168168

169169
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime
145145
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
146146

147147
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
148-
stackalloc char[length] :
148+
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
149149
(propertyArray = ArrayPool<char>.Shared.Rent(length));
150150

151151
JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
@@ -168,7 +168,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Date
168168
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
169169

170170
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
171-
stackalloc byte[length] :
171+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
172172
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
173173

174174
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTimeOffset.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime
144144
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
145145

146146
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
147-
stackalloc char[length] :
147+
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
148148
(propertyArray = ArrayPool<char>.Shared.Rent(length));
149149

150150
JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
@@ -167,7 +167,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Date
167167
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
168168

169169
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
170-
stackalloc byte[length] :
170+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
171171
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
172172

173173
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Decimal.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, decimal
144144
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
145145

146146
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
147-
stackalloc char[length] :
147+
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
148148
(propertyArray = ArrayPool<char>.Shared.Rent(length));
149149

150150
JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
@@ -167,7 +167,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, deci
167167
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
168168

169169
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
170-
stackalloc byte[length] :
170+
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
171171
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
172172

173173
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

0 commit comments

Comments
 (0)