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: LookupPort translates negative numbers into valid port numbers on Windows without error. #13447

ChrisHines opened this issue Dec 1, 2015 · 4 comments


Copy link

@ChrisHines ChrisHines commented Dec 1, 2015

OS: Windows 7 Professional, SP1.
Go: go1.5.1 windows/amd64

The following program prints 65527 on Windows, but panics on Linux.

package main

import (

func main() {
    port, err := net.LookupPort("tcp", "-9")
    if err != nil {

Also "-2000000000" returns 27648 and "-4294967295" or numerically smaller returns 1.

Copy link

@bradfitz bradfitz commented Dec 1, 2015

/cc @alexbrainman

I guess this is the newLookupPort path?

func newLookupPort(network, service string) (int, error) {
        defer releaseThread()
        var stype int32
        switch network {
        case "tcp4", "tcp6":
                stype = syscall.SOCK_STREAM
        case "udp4", "udp6":
                stype = syscall.SOCK_DGRAM
        hints := syscall.AddrinfoW{
                Family:   syscall.AF_UNSPEC,
                Socktype: stype,
                Protocol: syscall.IPPROTO_IP,
        var result *syscall.AddrinfoW
        e := syscall.GetAddrInfoW(nil, syscall.StringToUTF16Ptr(service), &hints, &result)
        if e != nil {
                return 0, &DNSError{Err: os.NewSyscallError("getaddrinfow", e).Error(), Name: network + "/" + service}
        defer syscall.FreeAddrInfoW(result)
        if result == nil {
                return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service}
        addr := unsafe.Pointer(result.Addr)
        switch result.Family {
        case syscall.AF_INET:
                a := (*syscall.RawSockaddrInet4)(addr)
                return int(syscall.Ntohs(a.Port)), nil
        case syscall.AF_INET6:
                a := (*syscall.RawSockaddrInet6)(addr)
                return int(syscall.Ntohs(a.Port)), nil
        return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service}
Copy link

@alexbrainman alexbrainman commented Dec 1, 2015

Let me play with this a little bit.


Copy link

@alexbrainman alexbrainman commented Dec 2, 2015

@ChrisHines your program prinsts:

panic: invalid port -9

goroutine 1 [running]:
        /root/src/t/main.go:11 +0x9b

on my windows 7 amd64 (as expected). Mind you, I use current Go tip. We also have TestLookupPort in net package that checks that net.LookupPort("tcp", "-1") fails. Can you run that test to see if it PASSes? You can add your values to the test to see what is going on. You can also debug this yourself (since it is broken for you). Just add fmt.Printf or similar everywhere in net as you descend into net.LookupPort call. Please let us know how you went. Thank you.


Copy link
Contributor Author

@ChrisHines ChrisHines commented Dec 2, 2015

@alexbrainman This issue was fixed by b50b21d.

@ChrisHines ChrisHines closed this Dec 2, 2015
@golang golang locked and limited conversation to collaborators Dec 1, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants