Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for nil interfaces #251

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -36,6 +36,9 @@
for _, mb := range ms {
bidx += copy(b[bidx:], mb.Bytes())
}
if length == 0 {
return nil

Check warning on line 40 in util.go

View check run for this annotation

Codecov / codecov/patch

util.go#L40

Added line #L40 was not covered by tests
}
return &multiaddr{bytes: b}
}

Expand All @@ -59,6 +62,9 @@

// 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 @@ -80,6 +86,10 @@

// 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 @@ -117,6 +127,9 @@
// 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

Check warning on line 131 in util.go

View check run for this annotation

Codecov / codecov/patch

util.go#L131

Added line #L131 was not covered by tests
}
// Shortcut if we already have a component
if c, ok := m.(*Component); ok {
if cb(*c) {
Expand Down Expand Up @@ -159,6 +172,9 @@
// 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