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>
}()
}
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" {
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
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
// 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) {
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")
+}
// 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
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
}
}
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)
}
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
}
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
}
}
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
}
}
func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
- if r.PreferGo {
+ if r.preferGo() {
return lookupPortMap(network, service)
}