Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 3a0a133

Browse files
authored
Remove use of Dangerous* Span APIs and use MemoryMarshal instead (#2228)
1 parent 9cb1acd commit 3a0a133

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

src/Kestrel.Core/Internal/Http/Http1Connection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Diagnostics;
66
using System.IO.Pipelines;
7+
using System.Runtime.InteropServices;
78
using System.Text;
89
using System.Text.Encodings.Web.Utf8;
910
using Microsoft.AspNetCore.Http.Features;
@@ -341,7 +342,7 @@ private static unsafe string GetUtf8String(Span<byte> path)
341342
{
342343
// .NET 451 doesn't have pointer overloads for Encoding.GetString so we
343344
// copy to an array
344-
fixed (byte* pointer = &path.DangerousGetPinnableReference())
345+
fixed (byte* pointer = &MemoryMarshal.GetReference(path))
345346
{
346347
return Encoding.UTF8.GetString(pointer, path.Length);
347348
}

src/Kestrel.Core/Internal/Http/HttpParser.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO.Pipelines;
66
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
78
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
89

910
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
@@ -60,7 +61,7 @@ public unsafe bool ParseRequestLine(TRequestHandler handler, ReadableBuffer buff
6061
}
6162

6263
// Fix and parse the span
63-
fixed (byte* data = &span.DangerousGetPinnableReference())
64+
fixed (byte* data = &MemoryMarshal.GetReference(span))
6465
{
6566
ParseRequestLine(handler, data, span.Length);
6667
}
@@ -204,7 +205,7 @@ public unsafe bool ParseHeaders(TRequestHandler handler, ReadableBuffer buffer,
204205
var span = reader.Span;
205206
var remaining = span.Length - reader.Index;
206207

207-
fixed (byte* pBuffer = &span.DangerousGetPinnableReference())
208+
fixed (byte* pBuffer = &MemoryMarshal.GetReference(span))
208209
{
209210
while (remaining > 0)
210211
{
@@ -289,7 +290,7 @@ public unsafe bool ParseHeaders(TRequestHandler handler, ReadableBuffer buffer,
289290
var headerSpan = buffer.Slice(current, lineEnd).ToSpan();
290291
length = headerSpan.Length;
291292

292-
fixed (byte* pHeader = &headerSpan.DangerousGetPinnableReference())
293+
fixed (byte* pHeader = &MemoryMarshal.GetReference(headerSpan))
293294
{
294295
TakeSingleHeader(pHeader, length, handler);
295296
}

src/Kestrel.Core/Internal/Http/HttpRequestHeaders.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Collections.Generic;
77
using System.Runtime.CompilerServices;
8+
using System.Runtime.InteropServices;
89
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
910
using Microsoft.Extensions.Primitives;
1011
using Microsoft.Net.Http.Headers;
@@ -32,7 +33,7 @@ private void SetValueUnknown(string key, StringValues value)
3233

3334
public unsafe void Append(Span<byte> name, string value)
3435
{
35-
fixed (byte* namePtr = &name.DangerousGetPinnableReference())
36+
fixed (byte* namePtr = &MemoryMarshal.GetReference(name))
3637
{
3738
Append(namePtr, name.Length, value);
3839
}

src/Kestrel.Core/Internal/Http/PathNormalizer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Runtime.InteropServices;
67

78
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
89
{
@@ -14,7 +15,7 @@ public static class PathNormalizer
1415
// In-place implementation of the algorithm from https://tools.ietf.org/html/rfc3986#section-5.2.4
1516
public static unsafe int RemoveDotSegments(Span<byte> input)
1617
{
17-
fixed (byte* start = &input.DangerousGetPinnableReference())
18+
fixed (byte* start = &MemoryMarshal.GetReference(input))
1819
{
1920
var end = start + input.Length;
2021
return RemoveDotSegments(start, end);

src/Kestrel.Core/Internal/Http/PipelineExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO.Pipelines;
66
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
78

89
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
910
{
@@ -49,7 +50,7 @@ public unsafe static void WriteAsciiNoValidation(ref this WritableBufferWriter b
4950
if (sourceLength <= destLength)
5051
{
5152
fixed (char* input = data)
52-
fixed (byte* output = &dest.DangerousGetPinnableReference())
53+
fixed (byte* output = &MemoryMarshal.GetReference(dest))
5354
{
5455
EncodeAsciiCharsToBytes(input, output, sourceLength);
5556
}
@@ -72,7 +73,7 @@ public unsafe static void WriteNumeric(ref this WritableBufferWriter buffer, ulo
7273

7374
// Fast path, try copying to the available memory directly
7475
var simpleWrite = true;
75-
fixed (byte* output = &span.DangerousGetPinnableReference())
76+
fixed (byte* output = &MemoryMarshal.GetReference(span))
7677
{
7778
var start = output;
7879
if (number < 10 && bytesLeftInBlock >= 1)
@@ -152,7 +153,7 @@ private unsafe static void WriteAsciiMultiWrite(ref this WritableBufferWriter bu
152153
continue;
153154
}
154155

155-
fixed (byte* output = &buffer.Span.DangerousGetPinnableReference())
156+
fixed (byte* output = &MemoryMarshal.GetReference(buffer.Span))
156157
{
157158
EncodeAsciiCharsToBytes(inputSlice, output, writable);
158159
}

src/Kestrel.Core/Internal/Infrastructure/HttpUtilities.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Diagnostics;
66
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
78
using System.Text;
89
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
910

@@ -91,7 +92,7 @@ public static unsafe string GetAsciiStringNonNullCharacters(this Span<byte> span
9192
var asciiString = new string('\0', span.Length);
9293

9394
fixed (char* output = asciiString)
94-
fixed (byte* buffer = &span.DangerousGetPinnableReference())
95+
fixed (byte* buffer = &MemoryMarshal.GetReference(span))
9596
{
9697
// This version if AsciiUtilities returns null if there are any null (0 byte) characters
9798
// in the string
@@ -136,7 +137,7 @@ public static string GetAsciiStringEscaped(this Span<byte> span, int maxChars)
136137
[MethodImpl(MethodImplOptions.AggressiveInlining)]
137138
public static unsafe bool GetKnownMethod(this Span<byte> span, out HttpMethod method, out int length)
138139
{
139-
fixed (byte* data = &span.DangerousGetPinnableReference())
140+
fixed (byte* data = &MemoryMarshal.GetReference(span))
140141
{
141142
method = GetKnownMethod(data, span.Length, out length);
142143
return method != HttpMethod.Custom;
@@ -190,7 +191,7 @@ internal static unsafe HttpMethod GetKnownMethod(byte* data, int length, out int
190191
[MethodImpl(MethodImplOptions.AggressiveInlining)]
191192
public static unsafe bool GetKnownVersion(this Span<byte> span, out HttpVersion knownVersion, out byte length)
192193
{
193-
fixed (byte* data = &span.DangerousGetPinnableReference())
194+
fixed (byte* data = &MemoryMarshal.GetReference(span))
194195
{
195196
knownVersion = GetKnownVersion(data, span.Length);
196197
if (knownVersion != HttpVersion.Unknown)
@@ -249,7 +250,7 @@ internal static unsafe HttpVersion GetKnownVersion(byte* location, int length)
249250
[MethodImpl(MethodImplOptions.AggressiveInlining)]
250251
public static unsafe bool GetKnownHttpScheme(this Span<byte> span, out HttpScheme knownScheme)
251252
{
252-
fixed (byte* data = &span.DangerousGetPinnableReference())
253+
fixed (byte* data = &MemoryMarshal.GetReference(span))
253254
{
254255
return GetKnownHttpScheme(data, span.Length, out knownScheme);
255256
}

0 commit comments

Comments
 (0)