diff options
-rw-r--r-- | dhcpv6/dhcpv6.go | 3 | ||||
-rw-r--r-- | dhcpv6/dhcpv6_test.go | 14 |
2 files changed, 17 insertions, 0 deletions
diff --git a/dhcpv6/dhcpv6.go b/dhcpv6/dhcpv6.go index 7505dfc..576293d 100644 --- a/dhcpv6/dhcpv6.go +++ b/dhcpv6/dhcpv6.go @@ -79,6 +79,9 @@ func RelayMessageFromBytes(data []byte) (*RelayMessage, error) { func FromBytes(data []byte) (DHCPv6, error) { buf := uio.NewBigEndianBuffer(data) messageType := MessageType(buf.Read8()) + if buf.Error() != nil { + return nil, buf.Error() + } if messageType == MessageTypeRelayForward || messageType == MessageTypeRelayReply { return RelayMessageFromBytes(data) diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index fbf7761..476acd2 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "errors" "net" + "strconv" "testing" "github.com/insomniacslk/dhcp/iana" @@ -136,6 +137,19 @@ func TestFromAndToBytes(t *testing.T) { require.Equal(t, expected, toBytes) } +func TestFromBytesInvalid(t *testing.T) { + expected := [][]byte{ + {}, + } + t.Parallel() + for i, packet := range expected { + t.Run(strconv.Itoa(i), func(t *testing.T) { + _, err := FromBytes(packet) + require.Error(t, err) + }) + } +} + func TestNewAdvertiseFromSolicit(t *testing.T) { s := Message{ MessageType: MessageTypeSolicit, |