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>
This commit is contained in:
Mikio Hara
2017-06-28 12:07:11 +09:00
parent 455220fa52
commit 8663ed5da4
2 changed files with 42 additions and 0 deletions

View File

@@ -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)

View File

@@ -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()