Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: LookupHost sometimes returns slice which when modified ruins cache state. #14212

Closed
suharshs opened this issue Feb 3, 2016 · 5 comments
Closed
Milestone

Comments

@suharshs
Copy link
Contributor

suharshs commented Feb 3, 2016

To reproduce:
Machine must have localhost specified in the /etc/hosts file.
Machine must NOT have localhost entry in its DNS Server.

Running this code:

package main

import (
    "fmt"
    "net"
)

func main() {
    runClient()
    modifyAddrs("localhost")
    runClient()
}

func modifyAddrs(host string) []string {
    addrs, err := net.LookupHost(host)
    if err != nil {
            panic(err)
    }
    for i := range addrs {
            addrs[i] += "junk"
    }
    return addrs
}

func runClient() {
    _, err := net.Dial("tcp", "localhost:23000")
    fmt.Printf("err = %v\n", err)
}

Will output:

err = dial tcp 127.0.0.1:23000: getsockopt: connection refused
err = dial tcp: lookup localhost on 169.254.169.254:53: no such host

The first error message is able to resolve localhost but after modifying the returned addr slice, it no longer can.
I traced it down to the localStaticHost function returns a slice directly from the cache, rather than returning a copy of it.

I have a fix here: https://go-review.googlesource.com/#/c/19201/

@gopherbot
Copy link

CL https://golang.org/cl/19201 mentions this issue.

@bradfitz
Copy link
Contributor

bradfitz commented Feb 3, 2016

Is this a regression from Go 1.5?

@suharshs
Copy link
Contributor Author

suharshs commented Feb 3, 2016

I observed it in Go 1.5.3 and it also exists at HEAD.

@bradfitz bradfitz added this to the Go1.7 milestone Feb 3, 2016
@bradfitz
Copy link
Contributor

bradfitz commented Feb 3, 2016

In that case (since it's not a regression and lived with it in Go 1.5) we can fix it when the Go 1.7 dev tree opens up.

/cc @mikioh

@mikioh
Copy link
Contributor

mikioh commented Feb 4, 2016

Looks like this is one of long standing issues, actually since r56?

d6054fc
a4f6ed3

PS: Please change the test function names from "runTestSubject" to "testSubject" once the tree opens.

@golang golang locked and limited conversation to collaborators Feb 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants