TechnitiumSoftware/DnsServer · error · InvalidDomainNameException

Invalid domain name [{domain}]: length cannot exceed 255 byt

Error message

Invalid domain name [{domain}]: length cannot exceed 255 bytes.

What it means

Thrown by DomainTree.ConvertToByteKey (the protected key serializer used by the zone/domain radix tree) when a domain name's string length exceeds 255 characters. 255 bytes is the hard RFC 1035 limit for a fully-qualified domain name wire representation; the library enforces it before serializing to the lookup key. Only thrown when throwException is true (the default).

Source

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

        }

        public DomainTree()
            : base(41)
        { }

        #endregion

        #region protected

        protected override byte[] ConvertToByteKey(string domain, bool throwException = true)
        {
            if (domain.Length == 0)
                return [];

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

                return null;
            }

            byte[] key = new byte[domain.Length + 1];
            int keyOffset = 0;
            int labelStart;
            int labelEnd = domain.Length - 1;
            int labelLength;
            int labelChar;
            byte labelKeyCode;
            int i;

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

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Validate domain.Length <= 255 before inserting/querying the tree.
  2. Reject or truncate the offending record at ingestion (zone load / cache insert) rather than letting it reach the tree.
  3. If probing for validity, call ConvertToByteKey with throwException=false to get null instead of an exception.

Example fix

// before
_tree.GetOrAdd(veryLongDomain, value);

// after
if (string.IsNullOrEmpty(domain) || domain.Length > 255)
    return; // skip invalid name
_tree.GetOrAdd(domain, value);
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidDomainLength(string d) => string.IsNullOrEmpty(d) || d.Length <= 255;

Type guard

static bool IsValidDomain(string d) => !string.IsNullOrEmpty(d) && d.Length <= 255 && d.Split('.').All(l => l.Length >= 1 && l.Length <= 63);

Prevention

When it happens

Trigger: Passing a domain name string whose .Length > 255 to any tree operation (TryGet, GetOrAdd, Remove) on a DomainTree-derived tree (AuthZoneTree, cache trees), with the default throwException=true.

Common situations: Storing or querying a malformed/truncated record name from a zone transfer, a misconfigured upstream that synthesizes absurdly long names, or a bug concatenating labels without bounds.

Related errors


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