Commit 6ef2be492e56707bc181017660b8aef6067834be
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.
| |   |
| 316 | 316 | if (arrayIndex < 0) |
| 317 | 317 | throw new ArgumentOutOfRangeException ("arrayIndex"); |
| 318 | 318 | |
| int index = 0; |
| for (int i = arrayIndex; i < array.Length; ++i) |
| { |
| TKey key = keyOrder[index++]; |
| array[i] = new KeyValuePair<TKey, TValue> (key, this.dict[key]); |
|
| for (int i = 0; i < this.keyOrder.Count; ++i) { |
| TKey key = keyOrder [i]; |
| array [arrayIndex++] = new KeyValuePair<TKey, TValue> (key, this.dict [key]); |
| 324 | 323 | } |
| 325 | 324 | } |
| 326 | 325 | |
| … | … | |
| 372 | 372 | if (arrayIndex < 0 || arrayIndex > array.Length) |
| 373 | 373 | throw new ArgumentOutOfRangeException ("arrayIndex"); |
| 374 | 374 | |
| int index = 0; |
| for (int i = arrayIndex; i < array.Length; ++i) |
| array[i] = this.odict[index++]; |
| for (int i = 0; i < this.odict.Count; ++i) |
| array [arrayIndex++] = this.odict [i]; |
| 378 | 377 | } |
| 379 | 378 | |
| 380 | 379 | public int Count |