From 5313c1ae22dce0208e1f5b1980dedef57e09ff10 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Mon, 8 Dec 2014 04:03:19 -0800 Subject: [PATCH] fix(swarm) Dial panic +/- if len(addresses) is 0, connSetup will be called with nil value for c. +/- avoid variable reassignment License: MIT Signed-off-by: Brian Tiger Chow --- net/swarm/swarm.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/net/swarm/swarm.go b/net/swarm/swarm.go index f840d4fff..c7291a048 100644 --- a/net/swarm/swarm.go +++ b/net/swarm/swarm.go @@ -132,6 +132,9 @@ func (s *Swarm) Dial(peer peer.Peer) (conn.Conn, error) { Peerstore: s.peers, } + if len(peer.Addresses()) == 0 { + return nil, errors.New("peer has no addresses") + } // try to connect to one of the peer's known addresses. // for simplicity, we do this sequentially. // A future commit will do this asynchronously. @@ -145,7 +148,7 @@ func (s *Swarm) Dial(peer peer.Peer) (conn.Conn, error) { return nil, err } - c, err = s.connSetup(c) + c2, err := s.connSetup(c) if err != nil { c.Close() return nil, err @@ -153,14 +156,14 @@ func (s *Swarm) Dial(peer peer.Peer) (conn.Conn, error) { // TODO replace the TODO ctx with a context passed in from caller log.Event(context.TODO(), "dial", peer) - return c, nil + return c2, nil } // GetConnection returns the connection in the swarm to given peer.ID func (s *Swarm) GetConnection(pid peer.ID) conn.Conn { s.connsLock.RLock() + defer s.connsLock.RUnlock() c, found := s.conns[u.Key(pid)] - s.connsLock.RUnlock() if !found { return nil