Bug 2023 - System.Uri.Equals(object) should not throw an exception (when passed an non-uri-string)
Summary: System.Uri.Equals(object) should not throw an exception (when passed an non-u...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 2.10.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-11-14 07:59 UTC by Christian Klauser
Modified: 2011-11-24 08:23 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Christian Klauser 2011-11-14 07:59:45 UTC
The implementation of the Equals method on Uri objects (System.Uri.Equals(System.Object)) throws an exception when passed a one character string.

Consider the following program:

public class Program
{
        public static void Main(string[] args)
        {
                var uri = new System.Uri("http://xkcd.com");
                System.Console.Write(uri.Equals("-"));
        }
}

Expected result: Prints "False"
Actual result:
Unhandled Exception: System.UriFormatException: Absolute URI is too short
  at System.Uri.Parse (UriKind kind, System.String uriString) [0x00027] in /usr/src/redhat/BUILD/mono-2.10.2/mcs/class/System/System/Uri.cs:1342
  at System.Uri.ParseUri (UriKind kind) [0x00000] in /usr/src/redhat/BUILD/mono-2.10.2/mcs/class/System/System/Uri.cs:1199
  at System.Uri..ctor (System.String uriString, Boolean dontEscape) [0x00059] in /usr/src/redhat/BUILD/mono-2.10.2/mcs/class/System/System/Uri.cs:219
  at System.Uri..ctor (System.String uriString) [0x00000] in <filename unknown>:0
  at System.Uri.Equals (System.Object comparant) [0x00024] in /usr/src/redhat/BUILD/mono-2.10.2/mcs/class/System/System/Uri.cs:846
  at Program.Main (System.String[] args) [0x0000b] in /home/ec2-user/uri-equals-bug.cs:6


According to microsoft's documentation [1] "Implementations of Equals must not throw exceptions."
In my particular case, System.Web.Mvc.DefaultModelBinder.GetPropertyValue (part of ASP.NET MVC3) makes use of System.Uri.Equals(System.Object) when I bind against a model object with a System.Uri field. So short of not using the library's data binding facilities or changing my model, there is no easy workaround.

[1]: http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx 
Section "Remarks"
Comment 1 Marek Safar 2011-11-24 08:23:02 UTC
Fixed in master