Skip to content

Commit 5aaba99

Browse files
authored
Fix issue with not resetting valueLength (#78890)
* Fix issue with not resetting valueLength #78881 was just merged with a small bug, in that valueLength wasn't reset before it was used a second time. If the typeName is > 64 chars, then this would have thrown a different exception than it should have down a couple lines.
1 parent 2d946d2 commit 5aaba99

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/LanguageServer/Protocol/Protocol/Internal/Converters/ImageElementConverter.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ public override ImageElement Read(ref Utf8JsonReader reader, Type typeToConvert,
3333

3434
if (reader.TokenType == JsonTokenType.PropertyName)
3535
{
36-
var valueLength = reader.HasValueSequence ? reader.ValueSequence.Length : reader.ValueSpan.Length;
37-
38-
var propertyNameLength = valueLength <= scratchChars.Length ? reader.CopyString(scratchChars) : -1;
39-
var propertyName = propertyNameLength >= 0 ? scratchChars[..propertyNameLength] : reader.GetString().AsSpan();
36+
var propertyName = reader.GetStringSpan(scratchChars);
4037

4138
reader.Read();
4239
switch (propertyName)
@@ -48,10 +45,9 @@ public override ImageElement Read(ref Utf8JsonReader reader, Type typeToConvert,
4845
automationName = reader.GetString();
4946
break;
5047
case ObjectContentConverter.TypeProperty:
51-
var typePropertyLength = valueLength <= scratchChars.Length ? reader.CopyString(scratchChars) : -1;
52-
var typeProperty = typePropertyLength >= 0 ? scratchChars[..typePropertyLength] : reader.GetString().AsSpan();
48+
var typePropertyValue = reader.GetStringSpan(scratchChars);
5349

54-
if (!typeProperty.SequenceEqual(nameof(ImageElement).AsSpan()))
50+
if (!typePropertyValue.SequenceEqual(nameof(ImageElement).AsSpan()))
5551
throw new JsonException($"Expected {ObjectContentConverter.TypeProperty} property value {nameof(ImageElement)}");
5652
break;
5753
default:

src/LanguageServer/Protocol/Protocol/Internal/Converters/ImageIdConverter.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public override ImageId Read(ref Utf8JsonReader reader, Type objectType, JsonSer
3434

3535
if (reader.TokenType == JsonTokenType.PropertyName)
3636
{
37-
var valueLength = reader.HasValueSequence ? reader.ValueSequence.Length : reader.ValueSpan.Length;
38-
39-
var propertyNameLength = valueLength <= scratchChars.Length ? reader.CopyString(scratchChars) : -1;
40-
var propertyName = propertyNameLength >= 0 ? scratchChars[..propertyNameLength] : reader.GetString().AsSpan();
37+
var propertyName = reader.GetStringSpan(scratchChars);
4138

4239
reader.Read();
4340
switch (propertyName)
@@ -49,10 +46,9 @@ public override ImageId Read(ref Utf8JsonReader reader, Type objectType, JsonSer
4946
id = reader.GetInt32();
5047
break;
5148
case ObjectContentConverter.TypeProperty:
52-
var typePropertyLength = valueLength <= scratchChars.Length ? reader.CopyString(scratchChars) : -1;
53-
var typeProperty = typePropertyLength >= 0 ? scratchChars[..typePropertyLength] : reader.GetString().AsSpan();
49+
var typePropertyValue = reader.GetStringSpan(scratchChars);
5450

55-
if (!typeProperty.SequenceEqual(nameof(ImageId).AsSpan()))
51+
if (!typePropertyValue.SequenceEqual(nameof(ImageId).AsSpan()))
5652
throw new JsonException($"Expected {ObjectContentConverter.TypeProperty} property value {nameof(ImageId)}");
5753
break;
5854
default:

src/LanguageServer/Protocol/Protocol/Internal/Converters/VSInternalExtensionUtilities.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.Text.Json;
78
using System.Text.Json.Serialization;
@@ -71,4 +72,26 @@ void AddOrReplaceConverter<TBase, TExtension>()
7172
converters.Add(new VSExtensionConverter<TBase, TExtension>());
7273
}
7374
}
75+
76+
/// <summary>
77+
/// Returns a string read from the <see cref="Utf8JsonReader"/>. If the string is small enough
78+
/// to fit into the provided <paramref name="scratchChars"/>, no allocations will be needed.
79+
/// </summary>
80+
internal static ReadOnlySpan<char> GetStringSpan(this ref readonly Utf8JsonReader reader, Span<char> scratchChars)
81+
{
82+
var valueLength = reader.HasValueSequence ? reader.ValueSequence.Length : reader.ValueSpan.Length;
83+
84+
if (valueLength <= scratchChars.Length)
85+
{
86+
// If the value fits into the scratch buffer, copy it there, and return a span from that.
87+
var actualLength = reader.CopyString(scratchChars);
88+
89+
return scratchChars.Slice(0, actualLength);
90+
}
91+
else
92+
{
93+
// Otherwise, ask the reader to allocate a string and return a span from that.
94+
return reader.GetString().AsSpan();
95+
}
96+
}
7497
}

0 commit comments

Comments
 (0)