Skip to content

Commit

Permalink
net: Resolver.LookupIP return error for empty string
Browse files Browse the repository at this point in the history
Fixes #53995

Change-Id: Ib0de237b57382feb6b8070f2310945aef6c7db01
Reviewed-on: https://go-review.googlesource.com/c/go/+/419734
Reviewed-by: Damien Neil <[email protected]>
Reviewed-by: David Chase <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Damien Neil <[email protected]>
  • Loading branch information
ianwoolf authored and neild committed Aug 26, 2022
1 parent acabf87 commit 333681d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/net/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,15 @@ func (r *Resolver) LookupIP(ctx context.Context, network, host string) ([]IP, er
default:
return nil, UnknownNetworkError(network)
}

if host == "" {
return nil, &DNSError{Err: errNoSuchHost.Error(), Name: host, IsNotFound: true}
}
addrs, err := r.internetAddrList(ctx, afnet, host)
if err != nil {
return nil, err
}

ips := make([]IP, 0, len(addrs))
for _, addr := range addrs {
ips = append(ips, addr.(*IPAddr).IP)
Expand Down
11 changes: 11 additions & 0 deletions src/net/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,17 @@ func TestLookupIPAddrConcurrentCallsForNetworks(t *testing.T) {
wg.Wait()
}

// Issue 53995: Resolver.LookupIP should return error for empty host name.
func TestResolverLookupIPWithEmptyHost(t *testing.T) {
_, err := DefaultResolver.LookupIP(context.Background(), "ip", "")
if err == nil {
t.Fatal("DefaultResolver.LookupIP for empty host success, want no host error")
}
if !strings.HasSuffix(err.Error(), errNoSuchHost.Error()) {
t.Fatalf("lookup error = %v, want %v", err, errNoSuchHost)
}
}

func TestWithUnexpiredValuesPreserved(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

Expand Down

0 comments on commit 333681d

Please sign in to comment.