Ver código fonte

Fix CopyTo argument validation

Fixes #2669.
Jon Skeet 8 anos atrás
pai
commit
bd29f86804

+ 16 - 0
csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs

@@ -498,6 +498,14 @@ namespace Google.Protobuf.Collections
             Assert.Throws<ArgumentNullException>(() => keys.Contains(null));
         }
 
+        [Test]
+        public void KeysCopyTo()
+        {
+            var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } };
+            var keys = map.Keys.ToArray(); // Uses CopyTo internally
+            CollectionAssert.AreEquivalent(new[] { "foo", "x" }, keys);
+        }
+
         [Test]
         public void ValuesContains()
         {
@@ -510,6 +518,14 @@ namespace Google.Protobuf.Collections
             Assert.IsFalse(values.Contains(null));
         }
 
+        [Test]
+        public void ValuesCopyTo()
+        {
+            var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } };
+            var values = map.Values.ToArray(); // Uses CopyTo internally
+            CollectionAssert.AreEquivalent(new[] { "bar", "y" }, values);
+        }
+
         [Test]
         public void ToString_StringToString()
         {

+ 2 - 2
csharp/src/Google.Protobuf/Collections/MapField.cs

@@ -715,7 +715,7 @@ namespace Google.Protobuf.Collections
                 {
                     throw new ArgumentOutOfRangeException(nameof(arrayIndex));
                 }
-                if (arrayIndex + Count  >= array.Length)
+                if (arrayIndex + Count > array.Length)
                 {
                     throw new ArgumentException("Not enough space in the array", nameof(array));
                 }
@@ -746,7 +746,7 @@ namespace Google.Protobuf.Collections
                 {
                     throw new ArgumentOutOfRangeException(nameof(index));
                 }
-                if (index + Count >= array.Length)
+                if (index + Count > array.Length)
                 {
                     throw new ArgumentException("Not enough space in the array", nameof(array));
                 }