Add checks on data length for A and AAAA records (#919)
* Add checks on data length for A and AAAA records Fixes panic when parsing A or AAAA records with no data * Add tests Field() on empty A/AAAA data * Refactor format test * Add return value check on format test
This commit is contained in:
parent
cc8cd02140
commit
d49c86087e
|
@ -31,6 +31,9 @@ func Field(r RR, i int) string {
|
||||||
switch reflect.ValueOf(r).Elem().Type().Field(i).Tag {
|
switch reflect.ValueOf(r).Elem().Type().Field(i).Tag {
|
||||||
case `dns:"a"`:
|
case `dns:"a"`:
|
||||||
// TODO(miek): Hmm store this as 16 bytes
|
// TODO(miek): Hmm store this as 16 bytes
|
||||||
|
if d.Len() < net.IPv4len {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
if d.Len() < net.IPv6len {
|
if d.Len() < net.IPv6len {
|
||||||
return net.IPv4(byte(d.Index(0).Uint()),
|
return net.IPv4(byte(d.Index(0).Uint()),
|
||||||
byte(d.Index(1).Uint()),
|
byte(d.Index(1).Uint()),
|
||||||
|
@ -42,6 +45,9 @@ func Field(r RR, i int) string {
|
||||||
byte(d.Index(14).Uint()),
|
byte(d.Index(14).Uint()),
|
||||||
byte(d.Index(15).Uint())).String()
|
byte(d.Index(15).Uint())).String()
|
||||||
case `dns:"aaaa"`:
|
case `dns:"aaaa"`:
|
||||||
|
if d.Len() < net.IPv6len {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
return net.IP{
|
return net.IP{
|
||||||
byte(d.Index(0).Uint()),
|
byte(d.Index(0).Uint()),
|
||||||
byte(d.Index(1).Uint()),
|
byte(d.Index(1).Uint()),
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFieldEmptyAOrAAAAData(t *testing.T) {
|
||||||
|
res := Field(new(A), 1)
|
||||||
|
if res != "" {
|
||||||
|
t.Errorf("expected empty string but got %v", res)
|
||||||
|
}
|
||||||
|
res = Field(new(AAAA), 1)
|
||||||
|
if res != "" {
|
||||||
|
t.Errorf("expected empty string but got %v", res)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue