From bd29f86804407c583cd02fbf310191408176c0f4 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Mon, 6 Feb 2017 14:48:22 +0000 Subject: Fix CopyTo argument validation Fixes #2669. --- .../src/Google.Protobuf.Test/Collections/MapFieldTest.cs | 16 ++++++++++++++++ csharp/src/Google.Protobuf/Collections/MapField.cs | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs index 9c845907..9d3d69af 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs @@ -498,6 +498,14 @@ namespace Google.Protobuf.Collections Assert.Throws(() => keys.Contains(null)); } + [Test] + public void KeysCopyTo() + { + var map = new MapField { { "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 { { "foo", "bar" }, { "x", "y" } }; + var values = map.Values.ToArray(); // Uses CopyTo internally + CollectionAssert.AreEquivalent(new[] { "bar", "y" }, values); + } + [Test] public void ToString_StringToString() { diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index 537ce261..ef5651c9 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/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)); } -- cgit v1.2.3