diff options
-rw-r--r-- | dhcpv4/option_routes.go | 3 | ||||
-rw-r--r-- | dhcpv4/option_routes_test.go | 14 |
2 files changed, 12 insertions, 5 deletions
diff --git a/dhcpv4/option_routes.go b/dhcpv4/option_routes.go index 603273a..c98d481 100644 --- a/dhcpv4/option_routes.go +++ b/dhcpv4/option_routes.go @@ -38,6 +38,9 @@ func (r Route) Marshal(buf *uio.Lexer) { // Unmarshal implements uio.Unmarshaler. func (r *Route) Unmarshal(buf *uio.Lexer) error { maskSize := buf.Read8() + if maskSize > 32 { + return fmt.Errorf("invalid mask length %d in route option", maskSize) + } r.Dest = &net.IPNet{ IP: make([]byte, net.IPv4len), Mask: net.CIDRMask(int(maskSize), 32), diff --git a/dhcpv4/option_routes_test.go b/dhcpv4/option_routes_test.go index 19e331b..33f0ce7 100644 --- a/dhcpv4/option_routes_test.go +++ b/dhcpv4/option_routes_test.go @@ -16,9 +16,9 @@ func mustParseIPNet(s string) *net.IPNet { func TestParseRoutes(t *testing.T) { for _, tt := range []struct { - p []byte - want Routes - err error + p []byte + want Routes + wantErr bool }{ { p: []byte{32, 10, 2, 3, 4, 0, 0, 0, 0}, @@ -51,10 +51,14 @@ func TestParseRoutes(t *testing.T) { }, }, }, + { + p: []byte{64, 10, 2, 3, 4}, + wantErr: true, // Mask length 64 > 32 + }, } { var r Routes - if err := r.FromBytes(tt.p); err != tt.err { - t.Errorf("FromBytes(%v) = %v, want %v", tt.p, err, tt.err) + if err := r.FromBytes(tt.p); (err != nil) != tt.wantErr { + t.Errorf("FromBytes(%v) Unexpected error state: %v", tt.p, err) } if !reflect.DeepEqual(r, tt.want) { |