{"record":{"id":"c7482ecf4c69c7a5","repo":"TechnitiumSoftware/DnsServer","slug":"invalid-domain-name-domain-label-length-canno","errorCode":null,"errorMessage":"Invalid domain name [{domain}]: label length cannot be 0 byte.","messagePattern":"Invalid domain name \\[(.+?)\\]: label length cannot be 0 byte\\.","errorType":"validation","errorClass":"InvalidDomainNameException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/Trees/DomainTree.cs","lineNumber":141,"sourceCode":"            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\n                labelStart = domain.LastIndexOf('.', labelEnd);\n                labelLength = labelEnd - labelStart;\n\n                if (labelLength == 0)\n                {\n                    if (throwException)\n                        throw new InvalidDomainNameException(\"Invalid domain name [\" + domain + \"]: label length cannot be 0 byte.\");\n\n                    return null;\n                }\n\n                if (labelLength > 63)\n                {\n                    if (throwException)\n                        throw new InvalidDomainNameException(\"Invalid domain name [\" + domain + \"]: label length cannot exceed 63 bytes.\");\n\n                    return null;\n                }\n\n                if ((labelLength == 1) && (domain[labelStart + 1] == '*')) //[*]\n                {\n                    key[keyOffset++] = 1;\n                }\n                else\n                {","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/Trees/DomainTree.cs#L123-L159","documentation":"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.","triggerScenarios":"Passing a domain containing '..', '.', a leading '.', or a trailing '.' (after LastIndexOf('.') resolution yields labelLength == 0) to a DomainTree-derived tree operation with throwException=true.","commonSituations":"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.","solutions":["Normalize the domain before use: strip a single trailing root dot, reject consecutive dots, and trim leading dots.","Validate with a regex or a dedicated normalization routine prior to tree operations.","Use ConvertToByteKey(domain, throwException: false) for probing so an invalid name yields null."],"exampleFix":"// before\n_tree.GetOrAdd(\"example..com\", value);\n\n// after\nstring norm = domain.TrimEnd('.').Replace(\"..\", \".\");\nif (string.IsNullOrEmpty(norm) || norm.Contains(\"..\") || norm.StartsWith(\".\"))\n    return;\n_tree.GetOrAdd(norm, value);","handlingStrategy":"validation","validationCode":"string NormalizeDomain(string d)\n{\n    if (d is null) return null;\n    d = d.TrimEnd('.');\n    if (d.Contains(\"..\") || d.StartsWith(\".\") || d.EndsWith(\".\")) return null;\n    return d;\n}","typeGuard":"static bool HasNoEmptyLabels(string d) =>\n    !d.StartsWith(\".\") && !d.Contains(\"..\") && (!d.EndsWith(\".\") || d.Length == 1);","tryCatchPattern":null,"preventionTips":["Normalize user input: trim a single trailing root dot, reject consecutive/leading dots.","Validate zone/record names at the API boundary.","Probe with throwException=false for invalid-name detection without exceptions."],"tags":["dns","csharp","validation","rfc-1035","domain-name"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}