diff --git a/pkg/packet/bgp/bgp.go b/pkg/packet/bgp/bgp.go index f88e8973c..a7c4e2e87 100644 --- a/pkg/packet/bgp/bgp.go +++ b/pkg/packet/bgp/bgp.go @@ -9582,7 +9582,15 @@ func NewPrefixFromRouteFamily(afi uint16, safi uint8, prefixStr ...string) (pref family := AfiSafiToRouteFamily(afi, safi) f := func(s string) AddrPrefixInterface { - addr, net, _ := net.ParseCIDR(s) + addr, net, err := net.ParseCIDR(s) + if err != nil { + fmt.Printf("Invalid CIDR: %v", s) + switch family { + case RF_IPv4_UC, RF_IPv4_MC: + return NewIPAddrPrefix(0, "") + } + return NewIPv6AddrPrefix(0, "") + } len, _ := net.Mask.Size() switch family { case RF_IPv4_UC, RF_IPv4_MC: diff --git a/pkg/packet/bgp/bgp_test.go b/pkg/packet/bgp/bgp_test.go index b999d0993..49f51c067 100644 --- a/pkg/packet/bgp/bgp_test.go +++ b/pkg/packet/bgp/bgp_test.go @@ -88,6 +88,25 @@ func Test_IPAddrPrefixString(t *testing.T) { assert.Equal(t, "3343:faba:3903:128::/63", ipv6.String()) } +func Test_MalformedPrefixLookup(t *testing.T) { + assert := assert.New(t) + afi, safi := RouteFamilyToAfiSafi(RF_IPv4_UC) + prefix, err := NewPrefixFromRouteFamily(afi, safi, "129.6.128/22") + assert.Equal(nil, err) + assert.Equal("0.0.0.0/0", prefix.String()) + prefix, err = NewPrefixFromRouteFamily(afi, safi, "foo") + assert.Equal(nil, err) + assert.Equal("0.0.0.0/0", prefix.String()) + + afi, safi = RouteFamilyToAfiSafi(RF_IPv6_UC) + prefix, err = NewPrefixFromRouteFamily(afi, safi, "3343:faba:3903:128::::/63") + assert.Equal(nil, err) + assert.Equal("::/0", prefix.String()) + prefix, err = NewPrefixFromRouteFamily(afi, safi, "foo") + assert.Equal(nil, err) + assert.Equal("::/0", prefix.String()) +} + func Test_IPAddrDecode(t *testing.T) { r := IPAddrPrefixDefault{} b := make([]byte, 16)