Commit 8663ed5d authored by Mikio Hara's avatar Mikio Hara

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: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 455220fa
...@@ -280,6 +280,9 @@ type ResourceBody interface { ...@@ -280,6 +280,9 @@ type ResourceBody interface {
} }
func (r *Resource) pack(msg []byte, compression map[string]int) ([]byte, error) { 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 oldMsg := msg
r.Header.Type = r.Body.realType() r.Header.Type = r.Body.realType()
msg, length, err := r.Header.pack(msg, compression) msg, length, err := r.Header.pack(msg, compression)
......
...@@ -534,6 +534,45 @@ func TestBuilder(t *testing.T) { ...@@ -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) { func BenchmarkParsing(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment