[Mono-bugs] [Bug 383876] New: Array.LastIndexOf fails when the array is empty

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Fri Apr 25 13:52:41 EDT 2008


https://bugzilla.novell.com/show_bug.cgi?id=383876


           Summary: Array.LastIndexOf fails when the array is empty
           Product: Mono: Class Libraries
           Version: 1.9.0
          Platform: 64bit
        OS/Version: RHEL 5
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: System
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: jonbnews at hotmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


When an array is empty, the Array.LastIndexOf(array, value) method fails with
an ArgumentOutOfRangeException.  It should always return -1.

Here's a test case:

         string[] myArray = new string[] { }; // Empty array
         if (Array.LastIndexOf(myArray, "foo") == -1)
         {
            Console.WriteLine("Success");
         }

The expected output is "Success" (and that's what happens under Microsoft
NET).

I'm getting the following output:

 System.ArgumentOutOfRangeException : Argument is out of range.
  at System.Array.LastIndexOf[String] (System.String[] array, System.String
value, Int32 startIndex, Int32 count) [0x0007c] in
/tmp/mono-1.9/mcs/class/corlib/System/Array.cs:2147 
  at System.Array.LastIndexOf[String] (System.String[] array, System.String
value, Int32 startIndex) [0x00011] in
/tmp/mono-1.9/mcs/class/corlib/System/Array.cs:2128 
  at System.Array.LastIndexOf[String] (System.String[] array, System.String
value) [0x00011] in /tmp/mono-1.9/mcs/class/corlib/System/Array.cs:2120 


It appears that the method in Array.cs is incorrect:

                public static int LastIndexOf<T> (T [] array, T value)
                {
                        if (array == null)
                                throw new ArgumentNullException ("array");

                        return LastIndexOf<T> (array, value, array.Length - 1);
                }

It passes "array.Length - 1" as the startIndex, which will be -1 if the array
is empty.  That's an invalid start index and will result in the
ArgumentOutOfRangeException.

This also occurs for the List<T>.LastIndexOf, since that just calls through to
the Array.LastIndexOf method.


-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list