Merge pull request #84 from andrewtj/atj-escape

Fix packing label ending in escaped dot
This commit is contained in:
Miek Gieben 2014-02-15 08:06:09 +00:00
commit 979b2ea731
2 changed files with 17 additions and 2 deletions

6
msg.go
View File

@ -255,7 +255,7 @@ func packDomainName(s string, msg []byte, off int, compression map[string]int, c
// Emit sequence of counted strings, chopping at dots.
begin := 0
bs := []byte(s)
ro_bs, bs_fresh := s, true
ro_bs, bs_fresh, escaped_dot := s, true, false
for i := 0; i < ls; i++ {
if bs[i] == '\\' {
for j := i; j < ls-1; j++ {
@ -275,12 +275,13 @@ func packDomainName(s string, msg []byte, off int, compression map[string]int, c
}
ls -= 2
}
escaped_dot = bs[i] == '.'
bs_fresh = false
continue
}
if bs[i] == '.' {
if i > 0 && bs[i-1] == '.' {
if i > 0 && bs[i-1] == '.' && !escaped_dot {
// two dots back to back is not legal
return lenmsg, labels, ErrRdata
}
@ -334,6 +335,7 @@ func packDomainName(s string, msg []byte, off int, compression map[string]int, c
labels++
begin = i + 1
}
escaped_dot = false
}
// Root label is special
if len(bs) == 1 && bs[0] == '.' {

View File

@ -35,6 +35,19 @@ func TestDotInName(t *testing.T) {
}
}
func TestDotLastInLabel(t *testing.T) {
sample := "aa\\..au."
buf := make([]byte, 20)
_, err := PackDomainName(sample, buf, 0, nil, false)
if err != nil {
t.Fatalf("Unexpected error packing domain: %s", err)
}
dom, _, _ := UnpackDomainName(buf, 0)
if dom != sample {
t.Fatalf("Unpacked domain `%s' doesn't match packed domain", dom)
}
}
func TestTooLongDomainName(t *testing.T) {
l := "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqqrrrsssttt."
dom := l + l + l + l + l + l + l