Repository: dotnet/runtime
File: src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs (C#)

Code before:
                    // Narrow the query to IPv4.
                    addressFamily = AddressFamily.InterNetwork;
                }
            }

            resultOnFailure = null;
            return true;
        }

        /// <summary>
        /// Checks if the given host name matches a reserved name or is a subdomain of it.
        /// For example, IsReservedName("foo.localhost", "localhost") returns true.
        /// Also handles trailing dots: IsReservedName("foo.localhost.", "localhost") returns true.
        /// Returns false for malformed hostnames (starting with dot or containing consecutive dots).
        /// </summary>
        private static bool IsReservedName(string hostName, string reservedName)
        {
            // Reject malformed hostnames - let OS resolver handle them (and reject them)
            if (hostName.StartsWith('.') || hostName.Contains("..", StringComparison.Ordinal))
            {
                return false;
            }

            // Strip trailing dot if present (DNS root notation)
            ReadOnlySpan<char> hostSpan = hostName.AsSpan();
            if (hostSpan.Length > 0 && hostSpan[hostSpan.Length - 1] == '.')
            {
                hostSpan = hostSpan.Slice(0, hostSpan.Length - 1);
            }

            // Matches "reservedName" exactly, or "*.reservedName" (subdomain)
            return hostSpan.EndsWith(reservedName, StringComparison.OrdinalIgnoreCase) &&
                   (hostSpan.Length == reservedName.Length ||
                    hostSpan[hostSpan.Length - reservedName.Length - 1] == '.');
        }

        /// <summary>
        /// Checks if the given host name is a subdomain of localhost (e.g., "foo.localhost").
        /// Plain "localhost" or "localhost." returns false.
        /// </summary>
        private static bool IsLocalhostSubdomain(string hostName)
        {
            // Strip trailing dot for length comparison
            int length = hostName.Length;
            if (length > 0 && hostName[length - 1] == '.')
            {
                length--;
            }

            // Must be longer than "localhost" (not just equal with trailing dot)
            return length > "localhost".Length && IsReservedName(hostName, "localhost");

Review comment (suggestion):
```suggestion
            if (hostSpan.EndsWith('.'))
```

Code after:
                    // Narrow the query to IPv4.
                    addressFamily = AddressFamily.InterNetwork;
                }
            }

            resultOnFailure = null;
            return true;
        }

        private const string Localhost = "localhost";
        private const string InvalidDomain = "invalid";

        /// <summary>
        /// Checks if the given host name matches a reserved name or is a subdomain of it.
        /// For example, IsReservedName("foo.localhost", "localhost") returns true.
        /// Also handles trailing dots: IsReservedName("foo.localhost.", "localhost") returns true.
        /// Returns false for malformed hostnames (starting with dot or containing consecutive dots).
        /// </summary>
        private static bool IsReservedName(string hostName, string reservedName)
        {
            // Reject malformed hostnames - let OS resolver handle them (and reject them)
            if (hostName.StartsWith('.') || hostName.Contains("..", StringComparison.Ordinal))
            {
                return false;
            }

            // Strip trailing dot if present (DNS root notation)
            ReadOnlySpan<char> hostSpan = hostName.AsSpan();
            if (hostSpan.EndsWith('.'))
            {
                hostSpan = hostSpan.Slice(0, hostSpan.Length - 1);
            }

            // Matches "reservedName" exactly, or "*.reservedName" (subdomain)
            return hostSpan.EndsWith(reservedName, StringComparison.OrdinalIgnoreCase) &&
                   (hostSpan.Length == reservedName.Length ||
                    hostSpan[hostSpan.Length - reservedName.Length - 1] == '.');
        }

        /// <summary>
        /// Checks if the given host name is a subdomain of localhost (e.g., "foo.localhost").
        /// Plain "localhost" or "localhost." returns false.
        /// </summary>
        private static bool IsLocalhostSubdomain(string hostName)
        {
            // Strip trailing dot for length comparison
            int length = hostName.Length;
            if (hostName.EndsWith('.'))
            {
                length--;
            }