瀏覽代碼

Merge pull request #7701 from basvdlinden/safe_bytestring_copyfrom_span

annotate ByteString.CopyFrom(ReadOnlySpan<byte>) as SecuritySafeCritical
Jan Tattermusch 4 年之前
父節點
當前提交
9d9c670589
共有 2 個文件被更改,包括 13 次插入0 次删除
  1. 12 0
      csharp/src/Google.Protobuf.Test/ByteStringTest.cs
  2. 1 0
      csharp/src/Google.Protobuf/ByteString.cs

+ 12 - 0
csharp/src/Google.Protobuf.Test/ByteStringTest.cs

@@ -110,6 +110,18 @@ namespace Google.Protobuf
             Assert.AreEqual(10, bs[0]);
         }
 
+        [Test]
+        public void CopyFromReadOnlySpanCopiesContents()
+        {
+            byte[] data = new byte[1];
+            data[0] = 10;
+            ReadOnlySpan<byte> byteSpan = data;
+            var bs = ByteString.CopyFrom(byteSpan);
+            Assert.AreEqual(10, bs[0]);
+            data[0] = 5;
+            Assert.AreEqual(10, bs[0]);
+        }
+
         [Test]
         public void ToByteArrayCopiesContents()
         {

+ 1 - 0
csharp/src/Google.Protobuf/ByteString.cs

@@ -245,6 +245,7 @@ namespace Google.Protobuf
         /// are copied, so further modifications to the span will not
         /// be reflected in the returned <see cref="ByteString" />.
         /// </summary>
+        [SecuritySafeCritical]
         public static ByteString CopyFrom(ReadOnlySpan<byte> bytes)
         {
             return new ByteString(bytes.ToArray());