Properly check arguments in List<T> ICollection.CopyTo.
authorRodrigo Kumpera <kumpera@gmail.com>
Tue, 22 Feb 2011 17:48:32 +0000 (18:48 +0100)
committerRodrigo Kumpera <kumpera@gmail.com>
Tue, 22 Feb 2011 17:48:32 +0000 (18:48 +0100)
* List.cs (ICollection.CopyTo): Properly check
the array argument to match MS semantics.

Fixes #672907

mcs/class/corlib/System.Collections.Generic/List.cs
mcs/class/corlib/Test/System.Collections.Generic/ListTest.cs

index 4c56255d3a26aa862aed83056aa07603d65be7bc..7646b777ba77533e9f093590931c67355afa362c 100644 (file)
@@ -648,6 +648,10 @@ namespace System.Collections.Generic {
                
                void ICollection.CopyTo (Array array, int arrayIndex)
                {
+                       if (array == null)
+                               throw new ArgumentNullException ("array"); 
+                       if (array.Rank > 1 || array.GetLowerBound (0) != 0)
+                               throw new ArgumentException ("Array must be zero based and single dimentional", "array");
                        Array.Copy (_items, 0, array, arrayIndex, _size);
                }
                
index ba2273eb884670ffd4df49f45efc252d5b1026c7..1a866586f59cedfe95f06ee86c0e1ebbcddccc7e 100644 (file)
@@ -1265,6 +1265,48 @@ namespace MonoTests.System.Collections.Generic {
                        ((IDisposable) e4).Dispose ();
                        Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
                }
+
+               [Test] //bug #672907
+               public void ICollectionCopyToExceptions ()
+               {
+                       var l = new List <int> ();
+                       ICollection x = l;
+                       try {
+                               x.CopyTo (null, 0);
+                               Assert.Fail ("#1");
+                       } catch (Exception e) {
+                               Assert.IsTrue (e is ArgumentNullException, "#2");
+                       }
+
+                       try {
+                               x.CopyTo (new int [10], -1);
+                               Assert.Fail ("#3");
+                       } catch (Exception e) {
+                               Assert.IsTrue (e is ArgumentOutOfRangeException, "#4");
+                       }
+
+                       try {
+                               x.CopyTo (new int [10, 1], 0);
+                               Assert.Fail ("#5");
+                       } catch (Exception e) {
+                               Assert.IsTrue (e is ArgumentException, "#6");
+                       }
+
+                       try {
+                               x.CopyTo (Array.CreateInstance (typeof (int), new int [] { 10 }, new int[] { 1 }), 0);
+                               Assert.Fail ("#7");
+                       } catch (Exception e) {
+                               Assert.IsTrue (e is ArgumentException, "#8");
+                       }
+
+                       l.Add (10); l.Add (20);
+                       try {
+                               x.CopyTo (new int [1], 0);
+                               Assert.Fail ("#9");
+                       } catch (Exception e) {
+                               Assert.IsTrue (e is ArgumentException, "#10");
+                       }
+               }
        }
 }
 #endif