From 361c933a588c0d419b3ce39f90c0dd074c4d6228 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 29 May 2020 13:53:26 +0200 Subject: [PATCH] annotate ByteString.Span and ByteString.Memory as SecuritySafeCritical --- .../Google.Protobuf.Test/ByteStringTest.cs | 16 ++++++++++++++++ csharp/src/Google.Protobuf/ByteString.cs | 19 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs index 84e6341e95..0ef8d8f4e7 100644 --- a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs +++ b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs @@ -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); + } } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/ByteString.cs b/csharp/src/Google.Protobuf/ByteString.cs index 027c8d81c8..74dc865e9d 100644 --- a/csharp/src/Google.Protobuf/ByteString.cs +++ b/csharp/src/Google.Protobuf/ByteString.cs @@ -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 . /// No data is copied so this is the most efficient way of accessing. /// - public ReadOnlySpan Span => new ReadOnlySpan(bytes); + public ReadOnlySpan Span + { + [SecuritySafeCritical] + get + { + return new ReadOnlySpan(bytes); + } + } /// /// Provides read-only access to the data of this . /// No data is copied so this is the most efficient way of accessing. /// - public ReadOnlyMemory Memory => new ReadOnlyMemory(bytes); + public ReadOnlyMemory Memory + { + [SecuritySafeCritical] + get + { + return new ReadOnlyMemory(bytes); + } + } #endif ///