Bläddra i källkod

(Dictionary.CopyTo, HashKeyCollection.CopyTo, HashValueCollection.CopyTo):
Add some argument checks.

svn path=/trunk/mcs/; revision=42510

Raja R Harinath 21 år sedan
förälder
incheckning
d8affbf8fd

+ 2 - 0
mcs/class/corlib/System.Collections.Generic/ChangeLog

@@ -6,6 +6,8 @@
 	the index of the chain suitable for/containing the key.
 	(Item, Add, Remove): Simplify, and remove FIXMEs.
 	(Resize): Reuse linked list nodes from old table.
+	(Dictionary.CopyTo, HashKeyCollection.CopyTo, HashValueCollection.CopyTo):
+	Add some argument checks.
 
 2005-04-02  Ben Maurer  <[email protected]>
 

+ 28 - 7
mcs/class/corlib/System.Collections.Generic/Dictionary.cs

@@ -199,12 +199,17 @@ namespace System.Collections.Generic {
 	
 		void CopyTo (KeyValuePair<K, V> [] array, int index)
 		{
-			if (array.Length > _usedSlots)
+			if (array == null)
+				throw new ArgumentNullException ("array");
+			if (index < 0)
+				throw new ArgumentOutOfRangeException ("index");
+			if (index >= array.Length)
+				throw new ArgumentException ("index larger than largest valid index of array");
+			if (array.Length - index < _usedSlots)
 				throw new ArgumentException ("Destination array cannot hold the requested elements!");
 			
-			int i = 0;
 			foreach (KeyValuePair<K, V> kv in this)
-				array [i++] = kv;
+				array [index++] = kv;
 		}
 	
 		protected void Resize ()
@@ -635,10 +640,18 @@ namespace System.Collections.Generic {
 	
 			public void CopyTo (K [] array, int index)
 			{
+				if (array == null)
+					throw new ArgumentNullException ("array");
+				if (index < 0)
+					throw new ArgumentOutOfRangeException ("index");
+				if (index >= array.Length)
+					throw new ArgumentException ("index larger than largest valid index of array");
+				if (array.Length - index < _dictionary._usedSlots)
+					throw new ArgumentException ("Destination array cannot hold the requested elements!");
+
 				IEnumerable<K> enumerateThis = (IEnumerable<K>) this;
-				int i = 0;
 				foreach (K k in enumerateThis) {
-					array [i++] = k;
+					array [index++] = k;
 				}
 			}
 	
@@ -734,10 +747,18 @@ namespace System.Collections.Generic {
 	
 			public void CopyTo (V [] array, int index)
 			{
+				if (array == null)
+					throw new ArgumentNullException ("array");
+				if (index < 0)
+					throw new ArgumentOutOfRangeException ("index");
+				if (index >= array.Length)
+					throw new ArgumentException ("index larger than largest valid index of array");
+				if (array.Length - index < _dictionary._usedSlots)
+					throw new ArgumentException ("Destination array cannot hold the requested elements!");
+
 				IEnumerable<V> enumerateThis = (IEnumerable<V>) this;
-				int i = 0;
 				foreach (V v in enumerateThis) {
-					array [i++] = v;
+					array [index++] = v;
 				}
 			}