Skip to content

Commit

Permalink
netlist: validate netip.Prefix before appending to List
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Jun 30, 2022
1 parent 7a78075 commit 533ada5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pkg/matcher/netlist/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ func NewList() *List {
}
}

func mustValid(l []netip.Prefix) {
for i, prefix := range l {
if !prefix.IsValid() {
panic(fmt.Sprintf("invalid prefix at #%d", i))
}
}
}

// NewListFrom returns a *List using l as its initial contents.
// The new List takes ownership of l, and the caller should not use l after this call.
func NewListFrom(l []netip.Prefix) *List {
mustValid(l)
return &List{
e: l,
}
Expand All @@ -55,6 +64,7 @@ func NewListFrom(l []netip.Prefix) *List {
// Append appends new netip.Prefix(s) to the list.
// This modified the list. Caller must call List.Sort() before calling List.Contains()
func (list *List) Append(newNet ...netip.Prefix) {
mustValid(newNet)
list.e = append(list.e, newNet...)
list.sorted = false
}
Expand All @@ -74,9 +84,6 @@ func (list *List) Sort() {
}

for i, n := range list.e {
if !n.IsValid() {
panic("invalid prefix in list")
}
addr := netip.AddrFrom16(n.Addr().As16())
bits := n.Bits()
if n.Addr().Is4() {
Expand Down

0 comments on commit 533ada5

Please sign in to comment.