Add isRootLabel helper for packDomainName

This handles the bs == nil case internally.
This commit is contained in:
Tom Thorogood 2018-11-26 16:33:38 +10:30
parent 149f3c884f
commit 07ed56b1d6
No known key found for this signature in database
GPG Key ID: 86C63CDA416C6D2F
1 changed files with 14 additions and 5 deletions

19
msg.go
View File

@ -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