Skip to content

Commit

Permalink
check for nil interfaces (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo authored Jul 17, 2024
1 parent 94628cf commit bbdd1a5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions component.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (m *Component) UnmarshalJSON(data []byte) error {
}

func (c *Component) Equal(o Multiaddr) bool {
if o == nil {
return false
}
return bytes.Equal(c.bytes, o.Bytes())
}

Expand Down
7 changes: 7 additions & 0 deletions multiaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func NewMultiaddrBytes(b []byte) (a Multiaddr, err error) {

// Equal tests whether two multiaddrs are equal
func (m *multiaddr) Equal(m2 Multiaddr) bool {
if m2 == nil {
return false
}
return bytes.Equal(m.bytes, m2.Bytes())
}

Expand Down Expand Up @@ -139,6 +142,10 @@ func (m *multiaddr) Protocols() []Protocol {

// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
func (m *multiaddr) Encapsulate(o Multiaddr) Multiaddr {
if o == nil {
return m
}

mb := m.bytes
ob := o.Bytes()

Expand Down
22 changes: 22 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ func TestEqual(t *testing.T) {
}
}

// TestNilInterface makes sure funcs that accept a multiaddr interface don't
// panic if it's passed a nil interface.
func TestNilInterface(t *testing.T) {
m1 := newMultiaddr(t, "/ip4/127.0.0.1/udp/1234")
var m2 Multiaddr
m1.Equal(m2)
m1.Encapsulate(m2)
m1.Decapsulate(m2)

// Test components
c, _ := SplitFirst(m1)
c.Equal(m2)
c.Encapsulate(m2)
c.Decapsulate(m2)

// Util funcs
_ = Split(m2)
_, _ = SplitFirst(m2)
_, _ = SplitLast(m2)
ForEach(m2, func(c Component) bool { return true })
}

func TestStringToBytes(t *testing.T) {

testString := func(s string, h string) {
Expand Down
16 changes: 16 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func Join(ms ...Multiaddr) Multiaddr {
}
bidx += copy(b[bidx:], mb.Bytes())
}
if length == 0 {
return nil
}
return &multiaddr{bytes: b}
}

Expand All @@ -68,6 +71,9 @@ func StringCast(s string) Multiaddr {

// SplitFirst returns the first component and the rest of the multiaddr.
func SplitFirst(m Multiaddr) (*Component, Multiaddr) {
if m == nil {
return nil, nil
}
// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
return c, nil
Expand All @@ -89,6 +95,10 @@ func SplitFirst(m Multiaddr) (*Component, Multiaddr) {

// SplitLast returns the rest of the multiaddr and the last component.
func SplitLast(m Multiaddr) (Multiaddr, *Component) {
if m == nil {
return nil, nil
}

// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
return nil, c
Expand Down Expand Up @@ -126,6 +136,9 @@ func SplitLast(m Multiaddr) (Multiaddr, *Component) {
// component on which the callback first returns will be included in the
// *second* multiaddr.
func SplitFunc(m Multiaddr, cb func(Component) bool) (Multiaddr, Multiaddr) {
if m == nil {
return nil, nil
}
// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
if cb(*c) {
Expand Down Expand Up @@ -168,6 +181,9 @@ func SplitFunc(m Multiaddr, cb func(Component) bool) (Multiaddr, Multiaddr) {
// This function iterates over components *by value* to avoid allocating.
// Return true to continue iteration, false to stop.
func ForEach(m Multiaddr, cb func(c Component) bool) {
if m == nil {
return
}
// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
cb(*c)
Expand Down

0 comments on commit bbdd1a5

Please sign in to comment.