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 <brian@perfmode.com>
Brian Tiger Chow committed
Dec 8, 2014 at 04:03 UTC
5313c1ae22dce0208e1f5b1980dedef57e09ff10
1 file changed
+6
-3
net/swarm/swarm.go
+6
-3
@@ -132,6 +132,9 @@ func (s *Swarm) Dial(peer peer.Peer) (conn.Conn, error) {
132
Peerstore: s.peers,
133
}
134
135
+ if len(peer.Addresses()) == 0 {
136
+ return nil, errors.New("peer has no addresses")
137
+ }
138
// try to connect to one of the peer's known addresses.
139
// for simplicity, we do this sequentially.
140
// A future commit will do this asynchronously.
@@ -145,7 +148,7 @@ func (s *Swarm) Dial(peer peer.Peer) (conn.Conn, error) {
148
return nil, err
149
}
150
148
- c, err = s.connSetup(c)
151
+ c2, err := s.connSetup(c)
152
if err != nil {
153
c.Close()
154
return nil, err
@@ -153,14 +156,14 @@ func (s *Swarm) Dial(peer peer.Peer) (conn.Conn, error) {
156
157
// TODO replace the TODO ctx with a context passed in from caller
158
log.Event(context.TODO(), "dial", peer)
156
- return c, nil
159
+ return c2, nil
160
}
161
162
// GetConnection returns the connection in the swarm to given peer.ID
163
func (s *Swarm) GetConnection(pid peer.ID) conn.Conn {
164
s.connsLock.RLock()
165
+ defer s.connsLock.RUnlock()
166
c, found := s.conns[u.Key(pid)]
163
- s.connsLock.RUnlock()
167
168
if !found {
169
return nil