Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GetKnownHttpScheme "safe" #21204

Merged
merged 3 commits into from
Apr 28, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -430,20 +430,11 @@ internal static HttpVersion GetKnownVersionAndConfirmCR(this ReadOnlySpan<byte>
/// <param name="knownScheme">A reference to the known scheme, if the input matches any</param>
/// <returns>True when memory starts with known http or https schema</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool GetKnownHttpScheme(this Span<byte> span, out HttpScheme knownScheme)
public static bool GetKnownHttpScheme(this Span<byte> span, out HttpScheme knownScheme)
{
fixed (byte* data = span)
if (span.Length >= sizeof(ulong))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JIT isn't always smart enough to match this manual check to the call to BinaryPrimitives below, so it could end up duplicating the bounds check. Suggest using BinaryPrimitives.TryReadUInt64LittleEndian instead, which guarantees that the bounds check will be emitted a maximum of once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raised issue for it dotnet/runtime#35348; will try the the Try version as you suggest

{
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;
var scheme = BinaryPrimitives.ReadUInt64LittleEndian(span);
if ((scheme & _mask7Chars) == _httpSchemeLong)
{
knownScheme = HttpScheme.Http;
Expand Down