Skip to content

Commit

Permalink
don't panic on invalid pfx as input
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed May 10, 2024
1 parent d46d9ec commit 413f814
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
32 changes: 32 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (t *Table[V]) rootNodeByVersion(is4 bool) *node[V] {
// The prefix must already be normalized!
func (t *Table[V]) Insert(pfx netip.Prefix, val V) {
t.init()
if !pfx.IsValid() {
return
}

// some values derived from pfx
ip, bits, is4 := pfxToValues(pfx)
Expand Down Expand Up @@ -110,6 +113,10 @@ func (t *Table[V]) Insert(pfx netip.Prefix, val V) {
// The prefix must already be normalized!
func (t *Table[V]) Update(pfx netip.Prefix, cb func(val V, ok bool) V) V {
t.init()
if !pfx.IsValid() {
var zero V
return zero
}

// some values derived from pfx
ip, bits, is4 := pfxToValues(pfx)
Expand Down Expand Up @@ -148,6 +155,9 @@ func (t *Table[V]) Update(pfx netip.Prefix, cb func(val V, ok bool) V) V {
//
// The prefix must already be normalized!
func (t *Table[V]) Get(pfx netip.Prefix) (val V, ok bool) {
if !pfx.IsValid() {
return
}
// some values derived from pfx
ip, bits, is4 := pfxToValues(pfx)

Expand Down Expand Up @@ -181,6 +191,9 @@ func (t *Table[V]) Get(pfx netip.Prefix) (val V, ok bool) {
//
// The prefix must already be normalized!
func (t *Table[V]) Delete(pfx netip.Prefix) {
if !pfx.IsValid() {
return
}
// some values derived from pfx
ip, bits, is4 := pfxToValues(pfx)

Expand Down Expand Up @@ -243,6 +256,10 @@ func (t *Table[V]) Delete(pfx netip.Prefix) {
// Lookup does a route lookup (longest prefix match) for IP and
// returns the associated value and true, or false if no route matched.
func (t *Table[V]) Lookup(ip netip.Addr) (val V, ok bool) {
if !ip.IsValid() {
return
}

is4 := ip.Is4()

n := t.rootNodeByVersion(is4)
Expand Down Expand Up @@ -296,6 +313,9 @@ func (t *Table[V]) Lookup(ip netip.Addr) (val V, ok bool) {
//
// The prefix must already be normalized!
func (t *Table[V]) LookupPrefix(pfx netip.Prefix) (val V, ok bool) {
if !pfx.IsValid() {
return
}
_, _, val, ok = t.lpmByPrefix(pfx)
return
}
Expand All @@ -311,6 +331,9 @@ func (t *Table[V]) LookupPrefix(pfx netip.Prefix) (val V, ok bool) {
// If LookupPrefixLPM is to be used for IP addresses,
// they must be converted to /32 or /128 prefixes.
func (t *Table[V]) LookupPrefixLPM(pfx netip.Prefix) (lpm netip.Prefix, val V, ok bool) {
if !pfx.IsValid() {
return
}
depth, baseIdx, val, ok := t.lpmByPrefix(pfx)

if ok {
Expand Down Expand Up @@ -393,6 +416,9 @@ func (t *Table[V]) lpmByPrefix(pfx netip.Prefix) (depth int, baseIdx uint, val V
//
// The prefix must already be normalized!
func (t *Table[V]) Subnets(pfx netip.Prefix) []netip.Prefix {
if !pfx.IsValid() {
return nil
}
// some needed values, see below
ip, bits, is4 := pfxToValues(pfx)

Expand Down Expand Up @@ -434,6 +460,9 @@ func (t *Table[V]) Subnets(pfx netip.Prefix) []netip.Prefix {
//
// The prefix must already be normalized!
func (t *Table[V]) Supernets(pfx netip.Prefix) []netip.Prefix {
if !pfx.IsValid() {
return nil
}
var result []netip.Prefix

// some needed values, see below
Expand Down Expand Up @@ -477,6 +506,9 @@ func (t *Table[V]) Supernets(pfx netip.Prefix) []netip.Prefix {
//
// The prefix must be normalized!
func (t *Table[V]) OverlapsPrefix(pfx netip.Prefix) bool {
if !pfx.IsValid() {
return false
}
// some needed values, see below
ip, bits, is4 := pfxToValues(pfx)

Expand Down
107 changes: 107 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,113 @@ var mpp = func(s string) netip.Prefix {
return pfx
}

func TestValid(t *testing.T) {
t.Parallel()

var tbl = new(Table[any])
var zero netip.Prefix
var testname string

testname = "Insert"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.Insert(zero, nil)
})

testname = "Delete"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.Delete(zero)
})

testname = "Update"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.Update(zero, func(v any, _ bool) any { return v })
})

testname = "Get"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.Get(zero)
})

testname = "LookupPrefix"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.LookupPrefix(zero)
})

testname = "LookupPrefixLPM"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.LookupPrefixLPM(zero)
})

testname = "Subnets"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.Subnets(zero)
})

testname = "Supernets"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.Supernets(zero)
})

testname = "OverlapsPrefix"
t.Run(testname, func(t *testing.T) {
defer func(testname string) {
if r := recover(); r != nil {
t.Fatalf("%s panics on invalid prefix input", testname)
}
}(testname)

tbl.OverlapsPrefix(zero)
})
}

func TestRegression(t *testing.T) {
t.Parallel()
// original comment by tailscale for ART,
Expand Down

0 comments on commit 413f814

Please sign in to comment.