Fix ArgumentOutOfRangeException in OrderedCollection.CopyTo().
authorJonathan Pryor <jonpryor@vt.edu>
Wed, 9 Jun 2010 14:05:55 +0000 (10:05 -0400)
committerJonathan Pryor <jonpryor@vt.edu>
Wed, 9 Jun 2010 14:05:55 +0000 (10:05 -0400)
commit6ef2be492e56707bc181017660b8aef6067834be
treef262e024b599cf37f3223cc70eed9f8b29f6e136
parentaddb7344fb08339e95bf3728e5fc6191c6b92faa
Fix ArgumentOutOfRangeException in OrderedCollection.CopyTo().

Consider:

  var dict = new OrderedDictionary<int, int>() {
    { 1, 1 },
    { 2, 4 },
    { 3, 9 },
  };
  int[] dest= new int [5];
dict.CopyTo (dest, 1);

In the above, we're copying the OrderedDictionary contents into the middle of
the `dest` array, which is perfectly acceptable.

However, this would result in an ArgumentOutOfRangeException, because CopyTo()
would loop until it hit the length of the destination array, which can be (is,
above) larger than the dictionary itself.

Result: ArgumentOutOfRangeException when indexing into the backing List<int>.

The fix is to instead loop until we hit the maximum index of the dictionary,
not the maximum size of the destination array.
src/Cadenza/Cadenza.Collections/OrderedDictionary.cs