]> Cypherpunks repositories - gostls13.git/commitdiff
net: treat a nil *Resolver as a zero one, as documented
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 19 Mar 2018 06:16:25 +0000 (09:16 +0300)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 19 Mar 2018 17:01:23 +0000 (17:01 +0000)
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>
src/net/conf.go
src/net/dnsclient_unix.go
src/net/lookup.go
src/net/lookup_test.go
src/net/lookup_unix.go
src/net/lookup_windows.go

index 2c21331a00521dea430eddc1268f89d54005c08b..d4bd56cc58e8b8dda8268d9da0a766c5cff18321 100644 (file)
@@ -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" {
index 7ca5d6027b5193e02b426a3c018dbce491c36e57..835957a37c2f6f6af85729196f6ae8662b5d2f20 100644 (file)
@@ -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
index dffbc016b2c0968cb8bccda3cc1c2dfc04a4e88c..000f4a31ae7deb45aeb56b56ce7eaadbff53dc04 100644 (file)
@@ -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) {
index 481c6d40aec74e2edeb32e78ca385a0d1b63351c..ca1d804d5084041a616c4ea985a43d869f45bcc6 100644 (file)
@@ -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")
+}
index 8e77b8c70507a8bb117af961c6c48d1770ca3665..0cf4c99e0c7c4c5c2920a8c1324ddc75159f0152 100644 (file)
@@ -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
                }
index ac1f9b431accff7b0b4d4e84353f25aa2be264b4..2e6f40d048e331ddbabf84fe9af5d1cd6c3d07a8 100644 (file)
@@ -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)
        }