Skip to content

System.Uri.ToString result is incorrect when combining custom scheme with DOS-style path containing multibyte Unicode character #94572

@chris-white-wtw

Description

@chris-white-wtw

Description

When an instance of Uri is constructed from a string such as "xyz:C:/£", the ToString method produces a string in which the path component incorrectly appears twice: "xyz:C:/£C:/£"

Other properties appear correct though.

Reproduction Steps

Create a console app with the following code:

using System;

var u = new Uri("xyz:C:/£");
Console.WriteLine(u.ToString());

Expected behavior

I'd expect ToString to produce something that Uri's constructor can re-parse to an equivalent Uri instance.

Actual behavior

ToString's result includes the path component of the URI twice. If the original string passed to Uri had escape sequences in then these are unescaped in the second copy of the path but not the first.

Regression?

Behaviour was different in .NET Core 3.1 and in .NET Framework 4.7.2, but also not correct: the result of ToString prepends // to the path (e.g. xyz://C:/£) in these versions of the runtime.

Known Workarounds

Using the .AbsoluteUri property may be an option, though this is escaped differently in general.

Configuration

.NET 6.0.24 / 7.0.5 / 8.0.0-preview.4.23259.5
Windows 10 (19045.3570)
x64

Other information

The bug only seems to occur if all the following are true:

  • A custom URI scheme
  • A path that starts X:/ (where X is any letter) - this is quite specific, in that e.g. X:\ and X|/ do not exhibit the bug
  • The path contains a character that has a multibyte UTF8 representation (whether %-encoded or not)

Metadata

Metadata

Assignees

Labels

area-System.Netbugin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions