[Mono-list] Possible bug in StringCollection.CopyTo

Artur Karazniewicz karaznie@acn.waw.pl
Sun, 12 Jan 2003 09:05:25 +0100


Hello,

Looks like here it is a little bug in
StringCollection.CopyTo implementation.
Here is simple test case:

StringCollection stringCollection = new StringCollection();
string[] array = new string[0];
stringCollection.CopyTo(array,0);

Code like above is in NAnt-0.7.9 release, but under
mono throws:

Unhandled Exception: System.ArgumentException: index
in <0x000fd> 00 System.Collections.Specialized.StringCollection:CopyTo 
(string[],int)
in <0x00062> 00 .test:Main (string[])

This is not true that argument index(=0) is invalid,
since both stringCollection and array are empty and index is 0.

I've looked at StringCollection.CopyTo implementation:

public void CopyTo(string[] array, int index) {
   if (array == null) {
     throw new ArgumentNullException("array");
   } else if (index < 0) {
     throw new ArgumentOutOfRangeException("index");
   } else if (array.Rank > 1) {
     throw new ArgumentException("array");
   } else if (index >= array.Length) {
     throw new ArgumentException("index");
   } else if (array.Length - index < count) {
     throw new ArgumentException("array");
   }

   Array.Copy(entries, 0, array, index, count);
}

problem is with

if (index >= array.Length) {
     throw new ArgumentException("index");
}

which is wrong when array.Length and index are both 0;

Artur.