Skip to content

Commit

Permalink
Handle nil input
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Oct 1, 2024
1 parent 67d5a9b commit 1e3618a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func (r *Resolver) getResolver(domain string) BasicResolver {
// Resolve resolves a DNS multiaddr. It will only resolve the first DNS component in the multiaddr.
// If you need to resolve multiple DNS components, you may call this function again with each returned address.
func (r *Resolver) Resolve(ctx context.Context, maddr ma.Multiaddr) ([]ma.Multiaddr, error) {
if maddr == nil {
return nil, nil
}

// Find the next dns component.
preDNS, maddr := ma.SplitFunc(maddr, func(c ma.Component) bool {
switch c.Protocol().Code {
Expand Down
13 changes: 13 additions & 0 deletions resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ func TestEmptyResult(t *testing.T) {
}
}

func TestNil(t *testing.T) {
ctx := context.Background()
resolver := makeResolver()

addrs, err := resolver.Resolve(ctx, nil)
if err != nil {
t.Error(err)
}
if len(addrs) > 0 {
t.Fatalf("expected [], got %+v", addrs)
}
}

func TestDnsaddrMatching(t *testing.T) {
ctx := context.Background()
resolver := makeResolver()
Expand Down

0 comments on commit 1e3618a

Please sign in to comment.