Open
Description
Description
Uri.ToString
throws ArgumentOutOfRangeException
for some inputs that pass Uri.TryCreate
. Same behavior for .Host
, .AbsoluteUri
, etc:
Reproduction Steps
if (Uri.TryCreate("\t/�", UriKind.Absolute, out var uri)) {
Console.WriteLine(uri.ToString()); // throws ArgumentOutOfRangeException
}
Expected behavior
Either for Uri.TryCreate
to fail, or for uri.ToString() to succeed
Actual behavior
System.ArgumentOutOfRangeException: length ('-8') must be a non-negative value. (Parameter 'length')
Actual value was -8.
at System.ArgumentOutOfRangeException.ThrowNegative[T](T value, String paramName)
at System.ArgumentOutOfRangeException.ThrowIfNegative[T](T value, String paramName)
at System.String.ThrowSubstringArgumentOutOfRange(Int32 startIndex, Int32 length)
at System.String.Substring(Int32 startIndex, Int32 length)
at System.Uri.CreateHostStringHelper(String str, Int32 idx, Int32 end, Flags& flags, String& scopeId)
at System.Uri.CreateHostString()
at System.Uri.GetComponentsHelper(UriComponents uriComponents, UriFormat uriFormat)
at System.Uri.ToString()
The negative number seems to depend on how many leading tab characters the input string has.
Regression?
.NET Core 3.1: uri.ToString() → file://%EF%BF%BD/%25EF%25BF%25BD
Any later .NET version (from .NET 5 onwards): ArgumentOutOfRangeException
at ToString()
Known Workarounds
if (Uri.TryCreate(..., out var uri) && uri is { Scheme: "http" or "https" })
if (Uri.TryCreate(..., out var uri) && uri is { Scheme: not "file" })
Configuration
.NET 9.0.0, macOS 15.1.1 arm64
Other information
No response
Activity