UTF8 decode: remove "unsafe" from a bunch of places that don't need it any more

pull/19511/head
mgravell 6 years ago
parent 3ab3f5e586
commit 0628990feb
  1. 6
      src/csharp/Grpc.Core.Api/Metadata.cs
  2. 2
      src/csharp/Grpc.Core.Api/Utils/EncodingExtensions.cs
  3. 6
      src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs
  4. 11
      src/csharp/Grpc.Core/Internal/WellKnownStrings.cs

@ -346,7 +346,7 @@ namespace Grpc.Core
/// Creates a binary value or ascii value metadata entry from data received from the native layer.
/// We trust C core to give us well-formed data, so we don't perform any checks or defensive copying.
/// </summary>
internal static unsafe Entry CreateUnsafe(string key, byte* source, int length)
internal static Entry CreateUnsafe(string key, IntPtr source, int length)
{
if (HasBinaryHeaderSuffix(key))
{
@ -358,13 +358,13 @@ namespace Grpc.Core
else
{ // create a local copy in a fresh array
arr = new byte[length];
Marshal.Copy(new IntPtr(source), arr, 0, length);
Marshal.Copy(source, arr, 0, length);
}
return new Entry(key, null, arr);
}
else
{
string s = length == 0 ? "" : EncodingASCII.GetString(source, length);
string s = EncodingASCII.GetString(source, length);
return new Entry(key, s, null);
}
}

@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using System.Text;
namespace Grpc.Core.Api.Utils
@ -27,6 +28,7 @@ namespace Grpc.Core.Api.Utils
/// <summary>
/// Converts <c>IntPtr</c> pointing to a encoded byte array to a <c>string</c> using the provided <c>Encoding</c>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe string GetString(this Encoding encoding, IntPtr ptr, int len)
{
return len == 0 ? "" : encoding.GetString((byte*)ptr.ToPointer(), len);

@ -50,7 +50,7 @@ namespace Grpc.Core.Internal
/// <summary>
/// Reads metadata from pointer to grpc_metadata_array
/// </summary>
public static unsafe Metadata ReadMetadataFromPtrUnsafe(IntPtr metadataArray)
public static Metadata ReadMetadataFromPtrUnsafe(IntPtr metadataArray)
{
if (metadataArray == IntPtr.Zero)
{
@ -66,12 +66,12 @@ namespace Grpc.Core.Internal
UIntPtr keyLen;
IntPtr keyPtr = Native.grpcsharp_metadata_array_get_key(metadataArray, index, out keyLen);
int keyLen32 = checked((int)keyLen.ToUInt32());
string key = WellKnownStrings.TryIdentify((byte*)keyPtr.ToPointer(), keyLen32)
string key = WellKnownStrings.TryIdentify(keyPtr, keyLen32)
?? Marshal.PtrToStringAnsi(keyPtr, keyLen32);
UIntPtr valueLen;
IntPtr valuePtr = Native.grpcsharp_metadata_array_get_value(metadataArray, index, out valueLen);
int len32 = checked((int)valueLen.ToUInt64());
metadata.Add(Metadata.Entry.CreateUnsafe(key, (byte*)valuePtr.ToPointer(), len32));
metadata.Add(Metadata.Entry.CreateUnsafe(key, valuePtr, len32));
}
return metadata;
}

@ -26,6 +26,17 @@ namespace Grpc.Core.Internal
return *(ushort*)value;
}
/// <summary>
/// Test whether the provided byte sequence is recognized as a well-known string; if
/// so, return a shared instance of that string; otherwise, return null
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe string TryIdentify(IntPtr source, int length)
{
return TryIdentify((byte*)source.ToPointer(), length);
}
/// <summary>
/// Test whether the provided byte sequence is recognized as a well-known string; if
/// so, return a shared instance of that string; otherwise, return null

Loading…
Cancel
Save