{"record":{"id":"90a7740d1aaa992b","repo":"TechnitiumSoftware/DnsServer","slug":"invalid-domain-name-domain-length-cannot-exce","errorCode":null,"errorMessage":"Invalid domain name [{domain}]: length cannot exceed 255 bytes.","messagePattern":"Invalid domain name \\[(.+?)\\]: length cannot exceed 255 bytes\\.","errorType":"validation","errorClass":"InvalidDomainNameException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/Trees/DomainTree.cs","lineNumber":116,"sourceCode":"        }\n\n        public DomainTree()\n            : base(41)\n        { }\n\n        #endregion\n\n        #region protected\n\n        protected override byte[] ConvertToByteKey(string domain, bool throwException = true)\n        {\n            if (domain.Length == 0)\n                return [];\n\n            if (domain.Length > 255)\n            {\n                if (throwException)\n                    throw new InvalidDomainNameException(\"Invalid domain name [\" + domain + \"]: length cannot exceed 255 bytes.\");\n\n                return null;\n            }\n\n            byte[] key = new byte[domain.Length + 1];\n            int keyOffset = 0;\n            int labelStart;\n            int labelEnd = domain.Length - 1;\n            int labelLength;\n            int labelChar;\n            byte labelKeyCode;\n            int i;\n\n            do\n            {\n                if (labelEnd < 0)\n                    labelEnd = 0;\n","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/Trees/DomainTree.cs#L98-L134","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate domain.Length <= 255 before inserting/querying the tree.","Reject or truncate the offending record at ingestion (zone load / cache insert) rather than letting it reach the tree.","If probing for validity, call ConvertToByteKey with throwException=false to get null instead of an exception."],"exampleFix":"// before\n_tree.GetOrAdd(veryLongDomain, value);\n\n// after\nif (string.IsNullOrEmpty(domain) || domain.Length > 255)\n    return; // skip invalid name\n_tree.GetOrAdd(domain, value);","handlingStrategy":"validation","validationCode":"bool IsValidDomainLength(string d) => string.IsNullOrEmpty(d) || d.Length <= 255;","typeGuard":"static bool IsValidDomain(string d) => !string.IsNullOrEmpty(d) && d.Length <= 255 && d.Split('.').All(l => l.Length >= 1 && l.Length <= 63);","tryCatchPattern":null,"preventionTips":["Validate domain.Length <= 255 before any tree operation.","Reject over-long names at ingestion (zone load / cache insert).","Use ConvertToByteKey(domain, throwException:false) when probing."],"tags":["dns","csharp","validation","rfc-1035","domain-name"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}