Fix CopyTo argument validation

Fixes #2669.
This commit is contained in:
Jon Skeet 2017-02-06 14:48:22 +00:00 committed by Jon Skeet
parent 39756643df
commit bd29f86804
2 changed files with 18 additions and 2 deletions

View file

@ -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()
{

View file

@ -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));
}