From ead45182aac1103a98dbbd068b89ee883cc7eb28 Mon Sep 17 00:00:00 2001 From: Bas van der Linden Date: Sun, 12 Jul 2020 14:43:42 +0200 Subject: [PATCH 1/3] annotate ByteString.CopyFrom(ReadOnlySpan bytes) as SecuritySafeCritical --- csharp/src/Google.Protobuf/ByteString.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/csharp/src/Google.Protobuf/ByteString.cs b/csharp/src/Google.Protobuf/ByteString.cs index 74dc865e9d43..e7d5f4dc83f2 100644 --- a/csharp/src/Google.Protobuf/ByteString.cs +++ b/csharp/src/Google.Protobuf/ByteString.cs @@ -245,6 +245,7 @@ public static ByteString CopyFrom(byte[] bytes, int offset, int count) /// are copied, so further modifications to the span will not /// be reflected in the returned . /// + [SecuritySafeCritical] public static ByteString CopyFrom(ReadOnlySpan bytes) { return new ByteString(bytes.ToArray()); From 12d7976055fc36b8f3aed1a881b79f784e5fd040 Mon Sep 17 00:00:00 2001 From: Bas van der Linden Date: Sun, 12 Jul 2020 15:12:18 +0200 Subject: [PATCH 2/3] Added test for ByteString.CopyFrom(ReadOnlySpan bytes) --- csharp/src/Google.Protobuf.Test/ByteStringTest.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs index 0ef8d8f4e798..e31ecd18398a 100644 --- a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs +++ b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs @@ -110,6 +110,18 @@ public void CopyFromByteArrayCopiesContents() Assert.AreEqual(10, bs[0]); } + [Test] + public void CopyFromReadOnlySpanCopiesContents() + { + byte[] data = new byte[1]; + data[0] = 10; + ReadOnlySpan byteSpan = data; + var bs = ByteString.CopyFrom(byteSpan); + Assert.AreEqual(10, bs[0]); + data[0] = 5; + Assert.AreEqual(10, bs[0]); + } + [Test] public void ToByteArrayCopiesContents() { @@ -249,5 +261,6 @@ public void GetContentsAsReadOnlyMemory() var copied = byteString.Memory.ToArray(); CollectionAssert.AreEqual(byteString, copied); } + } } \ No newline at end of file From 4c1bb4d07bc8f5d1aa03136eda6adcd8c92e07dd Mon Sep 17 00:00:00 2001 From: Bas van der Linden Date: Sun, 12 Jul 2020 15:16:33 +0200 Subject: [PATCH 3/3] Remove newline --- csharp/src/Google.Protobuf.Test/ByteStringTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs index e31ecd18398a..adbbc3ef5470 100644 --- a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs +++ b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs @@ -261,6 +261,5 @@ public void GetContentsAsReadOnlyMemory() var copied = byteString.Memory.ToArray(); CollectionAssert.AreEqual(byteString, copied); } - } } \ No newline at end of file