From 0b39c9759def1c0ff77bce6d8073d18e3b1364bf Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 14 Feb 2026 23:41:55 +0100 Subject: [PATCH] Fix empty host parsing regression --- .../System.Private.Uri/src/System/Uri.cs | 2 +- .../FunctionalTests/UriCreateStringTests.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.Uri/src/System/Uri.cs b/src/libraries/System.Private.Uri/src/System/Uri.cs index e188918eabb7ff..518bf1777455a6 100644 --- a/src/libraries/System.Private.Uri/src/System/Uri.cs +++ b/src/libraries/System.Private.Uri/src/System/Uri.cs @@ -3855,7 +3855,7 @@ str[delimiterIdx] is not ('/' or '\\') && // Here we have checked the syntax up to the end of host // The only thing that can cause an exception is the port value // Spend some (duplicated) cycles on that. - else if (hostDelimiter == ':') + else if (hostDelimiter == ':' && hostLength != 0) { if ((syntaxFlags & UriSyntaxFlags.MayHavePort) != 0) { diff --git a/src/libraries/System.Private.Uri/tests/FunctionalTests/UriCreateStringTests.cs b/src/libraries/System.Private.Uri/tests/FunctionalTests/UriCreateStringTests.cs index 83e9e79a54a715..c7d318d8a09fe7 100644 --- a/src/libraries/System.Private.Uri/tests/FunctionalTests/UriCreateStringTests.cs +++ b/src/libraries/System.Private.Uri/tests/FunctionalTests/UriCreateStringTests.cs @@ -345,6 +345,9 @@ public static IEnumerable Scheme_Authority_TestData() yield return new object[] { "unknown://h.-/", "unknown", "", "h.-", UriHostNameType.Basic, -1, true, false }; yield return new object[] { "unknown://h._", "unknown", "", "h._", UriHostNameType.Basic, -1, true, false }; yield return new object[] { "unknown://", "unknown", "", "", UriHostNameType.Basic, -1, true, true }; + yield return new object[] { "unknown:///path", "unknown", "", "", UriHostNameType.Basic, -1, true, true }; + yield return new object[] { "unknown://?query", "unknown", "", "", UriHostNameType.Basic, -1, true, true }; + yield return new object[] { "unknown://#fragment", "unknown", "", "", UriHostNameType.Basic, -1, true, true }; // Mailto yield return new object[] { "mailto:", "mailto", "", "", UriHostNameType.Basic, 25, true, true }; @@ -372,6 +375,9 @@ public static IEnumerable Scheme_Authority_TestData() yield return new object[] { "unknown:", "unknown", "", "", UriHostNameType.Unknown, -1, true, false }; yield return new object[] { "unknown:path", "unknown", "", "", UriHostNameType.Unknown, -1, true, false }; yield return new object[] { "unknown://host", "unknown", "", "host", UriHostNameType.Dns, -1, true, false }; + yield return new object[] { "unknown://a:80", "unknown", "", "a", UriHostNameType.Dns, 80, false, false }; + yield return new object[] { "unknown://ab:80", "unknown", "", "ab", UriHostNameType.Dns, 80, false, false }; + yield return new object[] { "unknown://host:80", "unknown", "", "host", UriHostNameType.Dns, 80, false, false }; yield return new object[] { "unknown://userinfo@host", "unknown", "userinfo", "host", UriHostNameType.Dns, -1, true, false }; yield return new object[] { "unknown://userinfo@host:80", "unknown", "userinfo", "host", UriHostNameType.Dns, 80, false, false }; yield return new object[] { "unknown://./", "unknown", "", ".", UriHostNameType.Basic, -1, true, false }; @@ -1274,6 +1280,17 @@ public static IEnumerable Create_String_Invalid_TestData() yield return new object[] { "uri://a:2147483648", UriKind.Absolute }; yield return new object[] { "uri://a:80:80", UriKind.Absolute }; + // Unknown scheme with no host and a port + yield return new object[] { "tcp://:11111", UriKind.Absolute }; + yield return new object[] { "udp://:11111", UriKind.Absolute }; + yield return new object[] { "unix://:11111", UriKind.Absolute }; + yield return new object[] { "unknown://:80", UriKind.Absolute }; + yield return new object[] { "unknown://:80/path", UriKind.Absolute }; + yield return new object[] { "unknown://:80?query", UriKind.Absolute }; + yield return new object[] { "unknown://:80#fragment", UriKind.Absolute }; + yield return new object[] { "unknown://userinfo@:80", UriKind.Absolute }; + yield return new object[] { "unknown://userinfo@", UriKind.Absolute }; + if (PlatformDetection.IsNotInvariantGlobalization) { // Invalid unicode