annotate ByteString.Span and ByteString.Memory as SecuritySafeCritical

This commit is contained in:
Jan Tattermusch 2020-05-29 13:53:26 +02:00
parent 9070389516
commit 361c933a58
2 changed files with 33 additions and 2 deletions

View File

@ -233,5 +233,21 @@ namespace Google.Protobuf
ByteString b2 = ByteString.CopyFrom(200, 1, 2, 3, 4);
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);
}
}
}

View File

@ -34,6 +34,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security;
using System.Text;
#if !NET35
using System.Threading;
@ -115,13 +116,27 @@ namespace Google.Protobuf
/// 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.
/// </summary>
public ReadOnlySpan<byte> Span => new ReadOnlySpan<byte>(bytes);
public ReadOnlySpan<byte> Span
{
[SecuritySafeCritical]
get
{
return new ReadOnlySpan<byte>(bytes);
}
}
/// <summary>
/// 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.
/// </summary>
public ReadOnlyMemory<byte> Memory => new ReadOnlyMemory<byte>(bytes);
public ReadOnlyMemory<byte> Memory
{
[SecuritySafeCritical]
get
{
return new ReadOnlyMemory<byte>(bytes);
}
}
#endif
/// <summary>