mirror of
https://github.com/golang/net.git
synced 2026-03-31 18:37:08 +09:00
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:
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user