From 34f5a12bfca203e402052056da7166b2f46b68ac Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 22 Aug 2014 08:46:24 +0000 Subject: [PATCH] Don't crash on emtpy string in compressionLenSearch Fixes #105 --- dns_test.go | 4 ++-- msg.go | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dns_test.go b/dns_test.go index f91b4597..3ad8a894 100644 --- a/dns_test.go +++ b/dns_test.go @@ -316,7 +316,7 @@ func TestMsgLength2(t *testing.T) { } } -func testMsgLengthCompressionMalformed(t *testing.T) { +func TestMsgLengthCompressionMalformed(t *testing.T) { // SOA with empty hostmaster, which is illegal soa := &SOA{Hdr: RR_Header{Name: ".", Rrtype: TypeSOA, Class: ClassINET, Ttl: 12345}, Ns: ".", @@ -329,7 +329,7 @@ func testMsgLengthCompressionMalformed(t *testing.T) { m := new(Msg) m.Compress = true m.Ns = []RR{soa} - m.Len() + m.Len() // Should not crash. } func BenchmarkMsgLength(b *testing.B) { diff --git a/msg.go b/msg.go index 6a7755c6..cab1842d 100644 --- a/msg.go +++ b/msg.go @@ -1702,6 +1702,9 @@ func compressionLenHelper(c map[string]int, s string) { func compressionLenSearch(c map[string]int, s string) (int, bool) { off := 0 end := false + if s == "" { // don't bork on bogus data + return 0, false + } for { if _, ok := c[s[off:]]; ok { return len(s[off:]), true