annotate ByteString.Span and ByteString.Memory as SecuritySafeCritical

pull/7596/head
Jan Tattermusch 5 years ago
parent 5f5efe50c5
commit a6d1ed1712
  1. 16
      csharp/src/Google.Protobuf.Test/ByteStringTest.cs
  2. 19
      csharp/src/Google.Protobuf/ByteString.cs

@ -233,5 +233,21 @@ namespace Google.Protobuf
ByteString b2 = ByteString.CopyFrom(200, 1, 2, 3, 4); ByteString b2 = ByteString.CopyFrom(200, 1, 2, 3, 4);
Assert.AreNotEqual(b1.GetHashCode(), b2.GetHashCode()); Assert.AreNotEqual(b1.GetHashCode(), b2.GetHashCode());
} }
[Test]
public void GetContentsAsReadOnlySpan()
{
var byteString = ByteString.CopyFrom(1, 2, 3, 4, 5);
var copied = byteString.Span.ToArray();
CollectionAssert.AreEqual(byteString, copied);
}
[Test]
public void GetContentsAsReadOnlyMemory()
{
var byteString = ByteString.CopyFrom(1, 2, 3, 4, 5);
var copied = byteString.Memory.ToArray();
CollectionAssert.AreEqual(byteString, copied);
}
} }
} }

@ -34,6 +34,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Security;
using System.Text; using System.Text;
#if !NET35 #if !NET35
using System.Threading; using System.Threading;
@ -115,13 +116,27 @@ namespace Google.Protobuf
/// Provides read-only access to the data of this <see cref="ByteString"/>. /// Provides read-only access to the data of this <see cref="ByteString"/>.
/// No data is copied so this is the most efficient way of accessing. /// No data is copied so this is the most efficient way of accessing.
/// </summary> /// </summary>
public ReadOnlySpan<byte> Span => new ReadOnlySpan<byte>(bytes); public ReadOnlySpan<byte> Span
{
[SecuritySafeCritical]
get
{
return new ReadOnlySpan<byte>(bytes);
}
}
/// <summary> /// <summary>
/// Provides read-only access to the data of this <see cref="ByteString"/>. /// Provides read-only access to the data of this <see cref="ByteString"/>.
/// No data is copied so this is the most efficient way of accessing. /// No data is copied so this is the most efficient way of accessing.
/// </summary> /// </summary>
public ReadOnlyMemory<byte> Memory => new ReadOnlyMemory<byte>(bytes); public ReadOnlyMemory<byte> Memory
{
[SecuritySafeCritical]
get
{
return new ReadOnlyMemory<byte>(bytes);
}
}
#endif #endif
/// <summary> /// <summary>

Loading…
Cancel
Save