Skip to content

Null pointer causing panics

I'm experiencing panics caused by line 1048 in pt.go. In the following function:

func DialOr(info *ServerInfo, addr, methodName string) (*net.TCPConn, error) {
	c, err := DialOrWithDialer(&net.Dialer{}, info, addr, methodName)
	return c.(*net.TCPConn), err
}

when DialOrWithDialer returns an error, c will be nil, but here we are not checking if c is nil or not. This causes the following error for me:

panic: interface conversion: net.Conn is nil, not *net.TCPConn

goroutine 3197 [running]:
gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib.DialOr(...)
        /root/go/pkg/mod/gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib@v1.5.0/pt.go:1048
main.serverHandler({0x90f760, 0xc000080320}, {0x912150, 0xc0003c5640}, 0xc00007c5c0)
        /root/src/lyrebird/cmd/lyrebird/lyrebird.go:255 +0x65e
created by main.serverAcceptLoop in goroutine 8
        /root/src/lyrebird/cmd/lyrebird/lyrebird.go:234 +0x15f

A safer implementation of this function might be:

func DialOr(info *ServerInfo, addr, methodName string) (*net.TCPConn, error) {
	c, err := DialOrWithDialer(&net.Dialer{}, info, addr, methodName)
	if c != nil {
		return c.(*net.TCPConn), err
	}
	return nil, err
}