mirror of
https://github.com/golang/go
synced 2024-11-18 12:54:44 -07:00
net: treat a nil *Resolver as a zero one, as documented
Add accessors that handle nil without crashing. Fixes #24330 Change-Id: If5fbbb6015ca8d65f620a06bad6e52de8cd896ad Reviewed-on: https://go-review.googlesource.com/101315 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
This commit is contained in:
parent
5a4e09837c
commit
5c3cb64b2e
@ -126,7 +126,7 @@ func (c *conf) hostLookupOrder(r *Resolver, hostname string) (ret hostLookupOrde
|
||||
}()
|
||||
}
|
||||
fallbackOrder := hostLookupCgo
|
||||
if c.netGo || (r != nil && r.PreferGo) {
|
||||
if c.netGo || r.preferGo() {
|
||||
fallbackOrder = hostLookupFilesDNS
|
||||
}
|
||||
if c.forceCgoLookupHost || c.resolv.unknownOpt || c.goos == "android" {
|
||||
|
@ -377,7 +377,7 @@ func (r *Resolver) lookup(ctx context.Context, name string, qtype dnsmessage.Typ
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
if nerr, ok := err.(Error); ok && nerr.Temporary() && r.StrictErrors {
|
||||
if nerr, ok := err.(Error); ok && nerr.Temporary() && r.strictErrors() {
|
||||
// If we hit a temporary error with StrictErrors enabled,
|
||||
// stop immediately instead of trying more names.
|
||||
break
|
||||
@ -565,7 +565,7 @@ func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, name string, order
|
||||
for range qtypes {
|
||||
racer := <-lane
|
||||
if racer.error != nil {
|
||||
if nerr, ok := racer.error.(Error); ok && nerr.Temporary() && r.StrictErrors {
|
||||
if nerr, ok := racer.error.(Error); ok && nerr.Temporary() && r.strictErrors() {
|
||||
// This error will abort the nameList loop.
|
||||
hitStrictError = true
|
||||
lastErr = racer.error
|
||||
|
@ -137,6 +137,9 @@ type Resolver struct {
|
||||
// TODO(bradfitz): Timeout time.Duration?
|
||||
}
|
||||
|
||||
func (r *Resolver) preferGo() bool { return r != nil && r.PreferGo }
|
||||
func (r *Resolver) strictErrors() bool { return r != nil && r.StrictErrors }
|
||||
|
||||
// LookupHost looks up the given host using the local resolver.
|
||||
// It returns a slice of that host's addresses.
|
||||
func LookupHost(host string) (addrs []string, err error) {
|
||||
|
@ -814,3 +814,27 @@ func TestLookupContextCancel(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 24330: treat the nil *Resolver like a zero value. Verify nothing
|
||||
// crashes if nil is used.
|
||||
func TestNilResolverLookup(t *testing.T) {
|
||||
if testenv.Builder() == "" {
|
||||
testenv.MustHaveExternalNetwork(t)
|
||||
}
|
||||
if runtime.GOOS == "nacl" {
|
||||
t.Skip("skip on nacl")
|
||||
}
|
||||
var r *Resolver = nil
|
||||
ctx := context.Background()
|
||||
|
||||
// Don't care about the results, just that nothing panics:
|
||||
r.LookupAddr(ctx, "8.8.8.8")
|
||||
r.LookupCNAME(ctx, "google.com")
|
||||
r.LookupHost(ctx, "google.com")
|
||||
r.LookupIPAddr(ctx, "google.com")
|
||||
r.LookupMX(ctx, "gmail.com")
|
||||
r.LookupNS(ctx, "google.com")
|
||||
r.LookupPort(ctx, "tcp", "smtp")
|
||||
r.LookupSRV(ctx, "service", "proto", "name")
|
||||
r.LookupTXT(ctx, "gmail.com")
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ func (r *Resolver) dial(ctx context.Context, network, server string) (Conn, erro
|
||||
// addresses, which Dial will use without a DNS lookup.
|
||||
var c Conn
|
||||
var err error
|
||||
if r.Dial != nil {
|
||||
if r != nil && r.Dial != nil {
|
||||
c, err = r.Dial(ctx, network, server)
|
||||
} else {
|
||||
var d Dialer
|
||||
@ -75,7 +75,7 @@ func (r *Resolver) dial(ctx context.Context, network, server string) (Conn, erro
|
||||
|
||||
func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {
|
||||
order := systemConf().hostLookupOrder(r, host)
|
||||
if !r.PreferGo && order == hostLookupCgo {
|
||||
if !r.preferGo() && order == hostLookupCgo {
|
||||
if addrs, err, ok := cgoLookupHost(ctx, host); ok {
|
||||
return addrs, err
|
||||
}
|
||||
@ -86,7 +86,7 @@ func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string,
|
||||
}
|
||||
|
||||
func (r *Resolver) lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
|
||||
if r.PreferGo {
|
||||
if r.preferGo() {
|
||||
return r.goLookupIP(ctx, host)
|
||||
}
|
||||
order := systemConf().hostLookupOrder(r, host)
|
||||
@ -102,7 +102,7 @@ func (r *Resolver) lookupIP(ctx context.Context, host string) (addrs []IPAddr, e
|
||||
}
|
||||
|
||||
func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
|
||||
if !r.PreferGo && systemConf().canUseCgo() {
|
||||
if !r.preferGo() && systemConf().canUseCgo() {
|
||||
if port, err, ok := cgoLookupPort(ctx, network, service); ok {
|
||||
if err != nil {
|
||||
// Issue 18213: if cgo fails, first check to see whether we
|
||||
@ -118,7 +118,7 @@ func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int
|
||||
}
|
||||
|
||||
func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) {
|
||||
if !r.PreferGo && systemConf().canUseCgo() {
|
||||
if !r.preferGo() && systemConf().canUseCgo() {
|
||||
if cname, err, ok := cgoLookupCNAME(ctx, name); ok {
|
||||
return cname, err
|
||||
}
|
||||
@ -308,7 +308,7 @@ func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error)
|
||||
}
|
||||
|
||||
func (r *Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error) {
|
||||
if !r.PreferGo && systemConf().canUseCgo() {
|
||||
if !r.preferGo() && systemConf().canUseCgo() {
|
||||
if ptrs, err, ok := cgoLookupPTR(ctx, addr); ok {
|
||||
return ptrs, err
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ func (r *Resolver) lookupIP(ctx context.Context, name string) ([]IPAddr, error)
|
||||
}
|
||||
|
||||
func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
|
||||
if r.PreferGo {
|
||||
if r.preferGo() {
|
||||
return lookupPortMap(network, service)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user