cmd/tailscaled, tsnet: don't return an interface containing a nil pointer

This tripped me up when I was testing something and wrote:

    if conn != nil {
        conn.Close()
    }

In netstack mode, when an error occurred we were getting a non-nil error
and a non-nil interface that contained a nil pointer. Instead, just
return a nil interface value.

Updates #cleanup

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: Id9ef3dd24529e0e8c53adc60ed914c31fbb10cc4
This commit is contained in:
Andrew Dunham 2024-01-05 11:23:30 -05:00
parent 29e98e18f8
commit 46bdbb3878
2 changed files with 14 additions and 2 deletions

View File

@ -511,7 +511,13 @@ func getLocalBackend(ctx context.Context, logf logger.Logf, logID logid.PublicID
return ok return ok
} }
dialer.NetstackDialTCP = func(ctx context.Context, dst netip.AddrPort) (net.Conn, error) { dialer.NetstackDialTCP = func(ctx context.Context, dst netip.AddrPort) (net.Conn, error) {
return ns.DialContextTCP(ctx, dst) // Note: don't just return ns.DialContextTCP or we'll
// return an interface containing a nil pointer.
tcpConn, err := ns.DialContextTCP(ctx, dst)
if err != nil {
return nil, err
}
return tcpConn, nil
} }
} }
if socksListener != nil || httpProxyListener != nil { if socksListener != nil || httpProxyListener != nil {

View File

@ -546,7 +546,13 @@ func (s *Server) start() (reterr error) {
return ok return ok
} }
s.dialer.NetstackDialTCP = func(ctx context.Context, dst netip.AddrPort) (net.Conn, error) { s.dialer.NetstackDialTCP = func(ctx context.Context, dst netip.AddrPort) (net.Conn, error) {
return ns.DialContextTCP(ctx, dst) // Note: don't just return ns.DialContextTCP or we'll
// return an interface containing a nil pointer.
tcpConn, err := ns.DialContextTCP(ctx, dst)
if err != nil {
return nil, err
}
return tcpConn, nil
} }
if s.Store == nil { if s.Store == nil {