TechnitiumSoftware/DnsServer · error · InvalidDomainNameException

Invalid domain name [{domain}]: label length cannot be 0 byt

Error message

Invalid domain name [{domain}]: label length cannot be 0 byte.

What it means

Thrown by DomainTree.ConvertToByteKey when a label within the domain name has zero length — i.e. two consecutive dots, a leading dot, or a trailing dot that resolves to an empty label. DNS labels must be 1–63 octets (RFC 1035), so an empty label is illegal outside the root. Only thrown when throwException is true.

Source

Thrown at DnsServerCore/Dns/Trees/DomainTree.cs:141

            int labelStart;
            int labelEnd = domain.Length - 1;
            int labelLength;
            int labelChar;
            byte labelKeyCode;
            int i;

            do
            {
                if (labelEnd < 0)
                    labelEnd = 0;

                labelStart = domain.LastIndexOf('.', labelEnd);
                labelLength = labelEnd - labelStart;

                if (labelLength == 0)
                {
                    if (throwException)
                        throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: label length cannot be 0 byte.");

                    return null;
                }

                if (labelLength > 63)
                {
                    if (throwException)
                        throw new InvalidDomainNameException("Invalid domain name [" + domain + "]: label length cannot exceed 63 bytes.");

                    return null;
                }

                if ((labelLength == 1) && (domain[labelStart + 1] == '*')) //[*]
                {
                    key[keyOffset++] = 1;
                }
                else
                {

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Normalize the domain before use: strip a single trailing root dot, reject consecutive dots, and trim leading dots.
  2. Validate with a regex or a dedicated normalization routine prior to tree operations.
  3. Use ConvertToByteKey(domain, throwException: false) for probing so an invalid name yields null.

Example fix

// before
_tree.GetOrAdd("example..com", value);

// after
string norm = domain.TrimEnd('.').Replace("..", ".");
if (string.IsNullOrEmpty(norm) || norm.Contains("..") || norm.StartsWith("."))
    return;
_tree.GetOrAdd(norm, value);
Defensive patterns

Strategy: validation

Validate before calling

string NormalizeDomain(string d)
{
    if (d is null) return null;
    d = d.TrimEnd('.');
    if (d.Contains("..") || d.StartsWith(".") || d.EndsWith(".")) return null;
    return d;
}

Type guard

static bool HasNoEmptyLabels(string d) =>
    !d.StartsWith(".") && !d.Contains("..") && (!d.EndsWith(".") || d.Length == 1);

Prevention

When it happens

Trigger: Passing a domain containing '..', '.', a leading '.', or a trailing '.' (after LastIndexOf('.') resolution yields labelLength == 0) to a DomainTree-derived tree operation with throwException=true.

Common situations: User-entered zone/record names not normalized (e.g. 'example..com', '.example.com', 'example.com.'), malformed zone file import, or string concatenation producing empty labels.

Related errors


AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13). Data as JSON: /api/errors/c7482ecf4c69c7a5. Report an issue: GitHub.