Skip to content

Uri.TryCreate throws IndexOutOfRangeException #28614

Closed

Description

@Metalnem

Uri.TryCreate is a non-throwing method, but for some inputs it can throw IndexOutOfRangeException. Here's the full program that reproduces this:

using System;
using System.Text;

namespace Test
{
  public class Program
  {
    public static void Main(string[] args)
    {
      var b = new byte[] {
        92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
        92, 226, 128, 174, 47, 255, 11, 117, 102, 112, 10
      };

      var s = Encoding.UTF8.GetString(b);
      Uri.TryCreate(s, UriKind.RelativeOrAbsolute, out _);
    }
  }
}

The stack trace:

Exception has occurred: CLR/System.IndexOutOfRangeException
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Private.Uri.dll: 'Index was outside the bounds of the array.'
   at System.Uri.CreateUriInfo(Flags cF)
   at System.Uri.EnsureUriInfo()
   at System.Uri.ParseRemaining()
   at System.Uri.EnsureParseRemaining()
   at System.Uri.InitializeUri(ParsingError err, UriKind uriKind, UriFormatException& e)
   at System.Uri.CreateHelper(String uriString, Boolean dontEscape, UriKind uriKind, UriFormatException& e)
   at System.Uri.TryCreate(String uriString, UriKind uriKind, Uri& result)

The environment:

.NET Core SDK (reflecting any global.json):
 Version:   2.2.103
 Commit:    8edbc2570a

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx.10.14-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.2.103/

Found via SharpFuzz.

Activity

  1. davidsh commented on Feb 4, 2019

    @davidsh
    Contributor
  2. rmkerr commented on Feb 4, 2019

    @rmkerr
    Contributor

    Hey @Metalnem, thanks for reporting this! The decoded version of that string is \\\\\\\\\\\\\\‮/��ufp
    which contains a Unicode bidirectional control character (and was incredibly difficult to get formatted correctly for this comment). With that in mind, I think this is a duplicate of #1487.

    On a different note, SharpFuzz looks like a fantastic project. I'd love to get it running regularly against System.Uri.

  3. danmoseley commented on Feb 5, 2019

    @danmoseley
    Contributor

    and was incredibly difficult to get formatted correctly for this comment

    😃

  4. davidsh commented on Feb 5, 2019

    @davidsh
    Contributor

    and was incredibly difficult to get formatted correctly for this comment

    In cases like that, I like pasting in screenshots.
    image

  5. Metalnem commented on Feb 5, 2019

    @Metalnem
    Author

    @rmkerr Yes, it looks like the duplicate of #1487. I was searching the issues for TryCreate, which is probably why I missed it. Regarding SharpFuzz: my plan is actually to test the important parts of the .NET Core standard library, and then figure out some way to automate the tests.

  6. rmkerr commented on Feb 5, 2019

    @rmkerr
    Contributor

    Sounds great. If you run into any other URI related issues feel free to open an issue and tag me so I can take a look. The only exception would be anything potentially security related -- in that case you should hold off opening an issue, and should email secure@microsoft.com instead.

  7. transferred this issue fromdotnet/corefxon Feb 1, 2020
  8. added this to the 3.0 milestone on Feb 1, 2020
  9. ghost locked as resolved and limited conversation to collaborators on Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions