From d49c86087ef1d6582f3c401cb2d4c1db5bad0fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20Fjellstr=C3=B6m?= Date: Tue, 12 Mar 2019 17:31:33 +0100 Subject: [PATCH] Add checks on data length for A and AAAA records (#919) * Add checks on data length for A and AAAA records Fixes panic when parsing A or AAAA records with no data * Add tests Field() on empty A/AAAA data * Refactor format test * Add return value check on format test --- format.go | 6 ++++++ format_test.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 format_test.go diff --git a/format.go b/format.go index 86057f99..0ec79f2f 100644 --- a/format.go +++ b/format.go @@ -31,6 +31,9 @@ func Field(r RR, i int) string { switch reflect.ValueOf(r).Elem().Type().Field(i).Tag { case `dns:"a"`: // TODO(miek): Hmm store this as 16 bytes + if d.Len() < net.IPv4len { + return "" + } if d.Len() < net.IPv6len { return net.IPv4(byte(d.Index(0).Uint()), byte(d.Index(1).Uint()), @@ -42,6 +45,9 @@ func Field(r RR, i int) string { byte(d.Index(14).Uint()), byte(d.Index(15).Uint())).String() case `dns:"aaaa"`: + if d.Len() < net.IPv6len { + return "" + } return net.IP{ byte(d.Index(0).Uint()), byte(d.Index(1).Uint()), diff --git a/format_test.go b/format_test.go new file mode 100644 index 00000000..d0dce4ff --- /dev/null +++ b/format_test.go @@ -0,0 +1,16 @@ +package dns + +import ( + "testing" +) + +func TestFieldEmptyAOrAAAAData(t *testing.T) { + res := Field(new(A), 1) + if res != "" { + t.Errorf("expected empty string but got %v", res) + } + res = Field(new(AAAA), 1) + if res != "" { + t.Errorf("expected empty string but got %v", res) + } +}