From 07ed56b1d62521002a4e0537de24766cde2e9d26 Mon Sep 17 00:00:00 2001 From: Tom Thorogood Date: Mon, 26 Nov 2018 16:33:38 +1030 Subject: [PATCH] Add isRootLabel helper for packDomainName This handles the bs == nil case internally. --- msg.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/msg.go b/msg.go index 3062bc4e..f713b735 100644 --- a/msg.go +++ b/msg.go @@ -275,7 +275,7 @@ loop: // Don't try to compress '.' // We should only compress when compress is true, but we should also still pick // up names that can be used for *future* compression(s). - if compression != nil && roBs[begin:] != "." { + if compression != nil && !isRootLabel(s, bs, begin, ls) { if p, ok := compression[roBs[begin:]]; ok { // The first hit is the longest matching dname // keep the pointer offset we get back and store @@ -313,10 +313,7 @@ loop: } // Root label is special - if bs == nil && len(s) == 1 && s[0] == '.' { - return off, labels, nil - } - if bs != nil && len(bs) == 1 && bs[0] == '.' { + if isRootLabel(s, bs, 0, ls) { return off, labels, nil } @@ -335,6 +332,18 @@ loop: return off + 1, labels, nil } +// isRootLabel returns whether s or bs, from off to end, is the root +// label ".". +// +// If bs is nil, s will be checked, otherwise bs will be checked. +func isRootLabel(s string, bs []byte, off, end int) bool { + if bs == nil { + return s[off:end] == "." + } + + return end-off == 1 && bs[off] == '.' +} + // Unpack a domain name. // In addition to the simple sequences of counted strings above, // domain names are allowed to refer to strings elsewhere in the