Closed
Description
Calling net.LookupPort with a service-name starting with a number, regardless of whether or not it is in /etc/services, incorrectly returns the numeric prefix of the service-name as port. In my case, I was trying to perform a lookup on "9pfs".
LookupPort (net/lookup.go) calls dtoi (net/parse.go). lookupPort is only called if dtoi returns that it was not successful, but dtoi is successful as long as the string starts with a digit, which makes it incapable of detecting if a string is a service or a port.
Test code
package main
import "fmt"
import "net"
func main() {
x, err := net.LookupPort("tcp", "9pfs")
fmt.Printf("Port: %d, err: %v\n", x, err)
x, err = net.LookupPort("tcp", "1234port")
fmt.Printf("Port: %d, err: %v\n", x, err)
x, err = net.LookupPort("tcp", "port1234")
fmt.Printf("Port: %d, err: %v\n", x, err)
x, err = net.LookupPort("tcp", "http")
fmt.Printf("Port: %d, err: %v\n", x, err)
}
Expected result
Port: 564, err: <nil>
Port: 0, err: lookup tcp/1234port: nodename nor servname provided, or not known
Port: 0, err: lookup tcp/port1234: nodename nor servname provided, or not known
Port: 80, err: <nil>
Actual result
Port: 9, err: <nil>
Port: 1234, err: <nil>
Port: 0, err: lookup tcp/port1234: nodename nor servname provided, or not known
Port: 80, err: <nil>