]> Cypherpunks repositories - gostls13.git/commitdiff
net: LookupAddr("127.0.0.1") is "localhost" not "localhost."
authorRuss Cox <rsc@golang.org>
Thu, 7 Jan 2016 01:37:05 +0000 (20:37 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 8 Jan 2016 03:14:47 +0000 (03:14 +0000)
Fixes #13564.

Change-Id: I30c827ef4a112fee21b8493a67d0227109e35072
Reviewed-on: https://go-review.googlesource.com/18384
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/dnsclient.go
src/net/lookup_test.go

index 0f4ef893877fd1dfddbc26f568785fed862d9905..98d1750a631bc82ee76bcf09d84cbad9c2a79c77 100644 (file)
@@ -165,8 +165,18 @@ func isDomainName(s string) bool {
 // trailing dot to match pure Go reverse resolver and all other lookup
 // routines.
 // See golang.org/issue/12189.
+// But we don't want to add dots for local names from /etc/hosts.
+// It's hard to tell so we settle on the heuristic that names without dots
+// (like "localhost" or "myhost") do not get trailing dots, but any other
+// names do.
 func absDomainName(b []byte) string {
-       if len(b) > 0 && b[len(b)-1] != '.' {
+       hasDots := false
+       for _, x := range b {
+               if x == '.' {
+                       hasDots = true
+               }
+       }
+       if hasDots && b[len(b)-1] != '.' {
                b = append(b, '.')
        }
        return string(b)
index fa2c6367db990092f0d0b2efdda3e3e2b4e62820..e10be9a210773e468ba247f3531ecfd043ec91a9 100644 (file)
@@ -436,9 +436,15 @@ func TestLookupDotsWithLocalSource(t *testing.T) {
                        t.Errorf("#%d: %v", i, err)
                        continue
                }
+               mode := "netgo"
+               if i == 1 {
+                       mode = "netcgo"
+               }
                for _, name := range names {
-                       if !strings.HasSuffix(name, ".") {
-                               t.Errorf("#%d: got %s; want name ending with trailing dot", i, name)
+                       if strings.Index(name, ".") == len(name)-1 { // "localhost" not "localhost."
+                               t.Errorf("%s: got %s; want %s", mode, name, name[:len(name)-1])
+                       } else if strings.Contains(name, ".") && !strings.HasSuffix(name, ".") { // "localhost.localdomain." not "localhost.localdomain"
+                               t.Errorf("%s: got %s; want name ending with trailing dot", mode, name)
                        }
                }
        }