Skip to content

Commit dda8967

Browse files
Remove unsafe from BytesOrdinalEqualsStringAndAscii (#31201)
1 parent 3535b83 commit dda8967

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/Servers/Kestrel/Core/test/AsciiDecoding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private void ExceptionThrownForZeroOrNonAscii(byte b)
6060
{
6161
var byteRange = Enumerable.Range(1, length).Select(x => (byte)x).ToArray();
6262
byteRange[position] = b;
63-
63+
6464
Assert.Throws<InvalidOperationException>(() => new Span<byte>(byteRange).GetAsciiStringNonNullCharacters());
6565
}
6666
}
@@ -147,7 +147,7 @@ static void Test(Span<byte> asciiBytes)
147147

148148
// Change byte back for next iteration, ensure is equal again
149149
asciiBytes[i] = b;
150-
Assert.True(StringUtilities.BytesOrdinalEqualsStringAndAscii(s, asciiBytes));
150+
Assert.True(StringUtilities.BytesOrdinalEqualsStringAndAscii(s, asciiBytes), s);
151151
}
152152
}
153153
}

src/Shared/ServerInfrastructure/StringUtilities.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ out Unsafe.AsRef<Vector<ushort>>(output),
389389
}
390390

391391
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
392-
public unsafe static bool BytesOrdinalEqualsStringAndAscii(string previousValue, ReadOnlySpan<byte> newValue)
392+
public static bool BytesOrdinalEqualsStringAndAscii(string previousValue, ReadOnlySpan<byte> newValue)
393393
{
394394
// previousValue is a previously materialized string which *must* have already passed validation.
395395
Debug.Assert(IsValidHeaderString(previousValue));
@@ -412,8 +412,8 @@ public unsafe static bool BytesOrdinalEqualsStringAndAscii(string previousValue,
412412
// This isn't problematic as we know the maximum length is max string length (from test above)
413413
// which is a signed value so half the size of the unsigned pointer value so we can safely add
414414
// a Vector<byte>.Count to it without overflowing.
415-
var count = (IntPtr)newValue.Length;
416-
var offset = (IntPtr)0;
415+
var count = (nint)newValue.Length;
416+
var offset = (nint)0;
417417

418418
// Get references to the first byte in the span, and the first char in the string.
419419
ref var bytes = ref MemoryMarshal.GetReference(newValue);
@@ -422,12 +422,12 @@ public unsafe static bool BytesOrdinalEqualsStringAndAscii(string previousValue,
422422
do
423423
{
424424
// If Vector not-accelerated or remaining less than vector size
425-
if (!Vector.IsHardwareAccelerated || (byte*)(offset + Vector<byte>.Count) > (byte*)count)
425+
if (!Vector.IsHardwareAccelerated || (offset + Vector<byte>.Count) > count)
426426
{
427427
if (IntPtr.Size == 8) // Use Intrinsic switch for branch elimination
428428
{
429429
// 64-bit: Loop longs by default
430-
while ((byte*)(offset + sizeof(long)) <= (byte*)count)
430+
while ((offset + sizeof(long)) <= count)
431431
{
432432
if (!WidenFourAsciiBytesToUtf16AndCompareToChars(
433433
ref Unsafe.Add(ref str, offset),
@@ -441,7 +441,7 @@ ref Unsafe.Add(ref str, offset + 4),
441441

442442
offset += sizeof(long);
443443
}
444-
if ((byte*)(offset + sizeof(int)) <= (byte*)count)
444+
if ((offset + sizeof(int)) <= count)
445445
{
446446
if (!WidenFourAsciiBytesToUtf16AndCompareToChars(
447447
ref Unsafe.Add(ref str, offset),
@@ -456,7 +456,7 @@ ref Unsafe.Add(ref str, offset),
456456
else
457457
{
458458
// 32-bit: Loop ints by default
459-
while ((byte*)(offset + sizeof(int)) <= (byte*)count)
459+
while ((offset + sizeof(int)) <= count)
460460
{
461461
if (!WidenFourAsciiBytesToUtf16AndCompareToChars(
462462
ref Unsafe.Add(ref str, offset),
@@ -468,7 +468,7 @@ ref Unsafe.Add(ref str, offset),
468468
offset += sizeof(int);
469469
}
470470
}
471-
if ((byte*)(offset + sizeof(short)) <= (byte*)count)
471+
if ((offset + sizeof(short)) <= count)
472472
{
473473
if (!WidenTwoAsciiBytesToUtf16AndCompareToChars(
474474
ref Unsafe.Add(ref str, offset),
@@ -479,7 +479,7 @@ ref Unsafe.Add(ref str, offset),
479479

480480
offset += sizeof(short);
481481
}
482-
if ((byte*)offset < (byte*)count)
482+
if (offset < count)
483483
{
484484
var ch = (char)Unsafe.Add(ref bytes, offset);
485485
if (((ch & 0x80) != 0) || Unsafe.Add(ref str, offset) != ch)
@@ -527,11 +527,11 @@ ref Unsafe.Add(ref str, offset),
527527
}
528528

529529
offset += Vector<byte>.Count;
530-
} while ((byte*)(offset + Vector<byte>.Count) <= (byte*)count);
530+
} while ((offset + Vector<byte>.Count) <= count);
531531

532532
// Vector path done, loop back to do non-Vector
533533
// If is a exact multiple of vector size, bail now
534-
} while ((byte*)offset < (byte*)count);
534+
} while (offset < count);
535535

536536
// If we get here (input is exactly a multiple of Vector length) then there are no inequalities via widening;
537537
// so the input bytes are both ascii and a match to the string if it was converted via Encoding.ASCII.GetString(...)
@@ -651,7 +651,7 @@ private static bool AllBytesInUInt16AreAscii(ushort value)
651651
return ((value & 0x8080u) == 0);
652652
}
653653

654-
private unsafe static bool IsValidHeaderString(string value)
654+
private static bool IsValidHeaderString(string value)
655655
{
656656
// Method for Debug.Assert to ensure BytesOrdinalEqualsStringAndAscii
657657
// is not called with an unvalidated string comparitor.

0 commit comments

Comments
 (0)