dns/dnsmessage: don't crash with nil resource body
Change-Id: I51969f70d4fc69829fd5a8bcd8a34b3be15b9db0
Reviewed-on: https://go-review.googlesource.com/46930
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/dns/dnsmessage/message.go b/dns/dnsmessage/message.go
index b3cadf2..3826c62 100644
--- a/dns/dnsmessage/message.go
+++ b/dns/dnsmessage/message.go
@@ -280,6 +280,9 @@
}
func (r *Resource) pack(msg []byte, compression map[string]int) ([]byte, error) {
+ if r.Body == nil {
+ return msg, &nestedError{"Resource", errors.New("nil resource body")}
+ }
oldMsg := msg
r.Header.Type = r.Body.realType()
msg, length, err := r.Header.pack(msg, compression)
diff --git a/dns/dnsmessage/message_test.go b/dns/dnsmessage/message_test.go
index bf826be..2b2c8f1 100644
--- a/dns/dnsmessage/message_test.go
+++ b/dns/dnsmessage/message_test.go
@@ -534,6 +534,45 @@
}
}
+func TestResourcePack(t *testing.T) {
+ for _, m := range []Message{
+ {
+ Questions: []Question{
+ {
+ Name: mustNewName("."),
+ Type: TypeAAAA,
+ Class: ClassINET,
+ },
+ },
+ Answers: []Resource{{ResourceHeader{}, nil}},
+ },
+ {
+ Questions: []Question{
+ {
+ Name: mustNewName("."),
+ Type: TypeAAAA,
+ Class: ClassINET,
+ },
+ },
+ Authorities: []Resource{{ResourceHeader{}, (*NSResource)(nil)}},
+ },
+ {
+ Questions: []Question{
+ {
+ Name: mustNewName("."),
+ Type: TypeA,
+ Class: ClassINET,
+ },
+ },
+ Additionals: []Resource{{ResourceHeader{}, nil}},
+ },
+ } {
+ if _, err := m.Pack(); err == nil {
+ t.Errorf("should fail: %v", m)
+ }
+ }
+}
+
func BenchmarkParsing(b *testing.B) {
b.ReportAllocs()