Browse Source

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>
Mikio Hara 8 năm trước cách đây
mục cha
commit
8663ed5da4
2 tập tin đã thay đổi với 42 bổ sung0 xóa
  1. 3 0
      dns/dnsmessage/message.go
  2. 39 0
      dns/dnsmessage/message_test.go

+ 3 - 0
dns/dnsmessage/message.go

@@ -280,6 +280,9 @@ type ResourceBody interface {
 }
 
 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)

+ 39 - 0
dns/dnsmessage/message_test.go

@@ -534,6 +534,45 @@ func TestBuilder(t *testing.T) {
 	}
 }
 
+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()