From 8b2f8088e926c6559a73a57f1c17a2b2e856f1be Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 28 Apr 2020 20:58:38 +0100 Subject: [PATCH] Make GetKnownHttpScheme "safe" (#21204) --- .../src/Internal/Infrastructure/HttpUtilities.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/HttpUtilities.cs b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/HttpUtilities.cs index cb59fa8aff..38cd1961ff 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/HttpUtilities.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/HttpUtilities.cs @@ -430,20 +430,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure /// A reference to the known scheme, if the input matches any /// True when memory starts with known http or https schema [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe bool GetKnownHttpScheme(this Span span, out HttpScheme knownScheme) + public static bool GetKnownHttpScheme(this Span span, out HttpScheme knownScheme) { - fixed (byte* data = span) + if (BinaryPrimitives.TryReadUInt64LittleEndian(span, out var scheme)) { - return GetKnownHttpScheme(data, span.Length, out knownScheme); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe bool GetKnownHttpScheme(byte* location, int length, out HttpScheme knownScheme) - { - if (length >= sizeof(ulong)) - { - var scheme = *(ulong*)location; if ((scheme & _mask7Chars) == _httpSchemeLong) { knownScheme = HttpScheme.Http;