Skip to content

Commit a70e8ab

Browse files
authored
Fix span write in InvariantCreateSortKeyOrdinalIgnoreCase (#105344)
1 parent 7d5fe6a commit a70e8ab

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Invariant.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ private static void InvariantCreateSortKeyOrdinalIgnoreCase(ReadOnlySpan<char> s
6666
if (char.IsLowSurrogate(cl))
6767
{
6868
SurrogateCasing.ToUpper(c, cl, out char hr, out char lr);
69-
BinaryPrimitives.WriteUInt16BigEndian(sortKey, hr);
70-
BinaryPrimitives.WriteUInt16BigEndian(sortKey, lr);
71-
i++;
69+
Span<byte> tmp = sortKey.Slice(0, 2 * sizeof(ushort)); // help with bounds check elimination
70+
BinaryPrimitives.WriteUInt16BigEndian(tmp, hr);
71+
BinaryPrimitives.WriteUInt16BigEndian(tmp.Slice(sizeof(ushort)), lr);
7272
sortKey = sortKey.Slice(2 * sizeof(ushort));
73+
i++;
7374
continue;
7475
}
7576
}

src/libraries/System.Runtime/tests/System.Globalization.Tests/Invariant/InvariantMode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ public unsafe void TestGetSortKeyLength_OverlongArgument(int inputLength)
773773
[InlineData("Hello", CompareOptions.IgnoreCase | CompareOptions.IgnoreWidth, "HELLO")]
774774
[InlineData("Hell\u00F6", CompareOptions.None, "Hell\u00F6")] // U+00F6 = LATIN SMALL LETTER O WITH DIAERESIS
775775
[InlineData("Hell\u00F6", CompareOptions.IgnoreCase, "HELL\u00D6")]
776+
[InlineData("Hell\uD82A\uDC15", CompareOptions.IgnoreCase, "HELL\uD82A\uDC15")] // U+D82A = High Surrogate; U+DC15 = Low Surrogate
776777
public unsafe void TestSortKey_FromSpan(string input, CompareOptions options, string expected)
777778
{
778779
byte[] expectedOutputBytes = GetExpectedInvariantOrdinalSortKey(expected);

0 commit comments

Comments
 (0)