Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Desktop] Uri(Uri, String) throws NullReferenceException for certain relativeUri values #25596

Closed
walro opened this issue Mar 23, 2018 · 4 comments
Assignees
Milestone

Comments

@walro
Copy link

walro commented Mar 23, 2018

Short reproduction from C# interactive in Visual Studio 2017 15.6.4 on Windows 10 1709 (I'd assume this runs under .NET 4.7.1):

var baseUri = new Uri("http://example.com");
var relativeUri = "ftp:%c3%b6";
new Uri(baseUri, relativeUri);
Object reference not set to an instance of an object.
  + System.Uri.CreateUriInfo(System.Uri.Flags)
  + System.Uri.EnsureUriInfo()
  + System.Uri.ParseRemaining()
  + System.Uri.EnsureParseRemaining()
  + System.Uri.CreateThis(string, bool, System.UriKind)
  + System.Uri.CreateUri(System.Uri, string, bool)

The exception type is unexpected, I would expect a UriFormatException to be thrown.

  • Changing : to %3a works fine
  • Changing %c3%b6 to a yields a better exception: System.UriFormatException: Invalid URI: The Authority/Host could not be parsed.

So there seems to be something going on with the UTF-8 decoding combined with special case when a protocol is at start of the string.

(The problem is the same under mono 5.4.1.6 on macOS)

[EDIT] Update code formatting by @karelz

@stephentoub
Copy link
Member

With .NET Core, this results in:

Unhandled Exception: System.UriFormatException: Invalid URI: The Authority/Host could not be parsed.
   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
   at System.Uri.CreateUri(Uri baseUri, String relativeUri, Boolean dontEscape)
   at System.Uri..ctor(Uri baseUri, String relativeUri)

as expected.

@davidsh
Copy link
Contributor

davidsh commented Mar 23, 2018

cc: @rmkerr

@rmkerr rmkerr self-assigned this Mar 23, 2018
@karelz karelz changed the title Uri(Uri, String) throws NullReferenceException for certain relativeUri values [Desktop] Uri(Uri, String) throws NullReferenceException for certain relativeUri values Mar 23, 2018
@rmkerr
Copy link
Contributor

rmkerr commented Mar 23, 2018

This will also throw the expected exception on .NET Framework 4.7.2

System.UriFormatException occurred
  Message=Invalid URI: The Authority/Host could not be parsed.
  StackTrace:
   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
   at System.Uri.CreateUri(Uri baseUri, String relativeUri, Boolean dontEscape)
   at System.Uri..ctor(Uri baseUri, String relativeUri)

This situation is actually quite interesting. For compatibility reasons, we usually allow creation of relative URIs when there is a scheme but no host. That's based on section 5.4.2 of the RFC:

   Some parsers allow the scheme name to be present in a relative
   reference if it is the same as the base URI scheme.  This is
   considered to be a loophole in prior specifications of partial URI
   [RFC1630].  Its use should be avoided but is allowed for backward
   compatibility.

      "http:g"        =  "http:g"         ; for strict parsers
                      /  "http://a/b/c/g" ; for backward compatibility

It looks like we're running into a situation where we're disallowing that behavior. I think it's worth looking into why that is. I think we should keep this issue open to track that investigation, but leave it in future for now.

@rmkerr
Copy link
Contributor

rmkerr commented May 2, 2018

In .NET Core Preview 2 and .NET Framework 4.7.2 this throws the expected UriFormatException. Closing as this appears to be fixed.

@rmkerr rmkerr closed this as completed May 2, 2018
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants