Skip to content

Commit e368dab

Browse files
p2p/kademlia: add nil guards to connWrapper and pool (#176)
1 parent 9c7e965 commit e368dab

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

p2p/kademlia/conn_pool.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kademlia
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"net"
78
"sync"
89
"sync/atomic"
@@ -123,56 +124,85 @@ func NewSecureServerConn(_ context.Context, tc credentials.TransportCredentials,
123124

124125
// Read implements net.Conn's Read interface
125126
func (conn *connWrapper) Read(b []byte) (n int, err error) {
127+
if conn == nil || conn.secureConn == nil {
128+
return 0, io.ErrClosedPipe
129+
}
126130
conn.mtx.Lock()
127131
defer conn.mtx.Unlock()
128132
return conn.secureConn.Read(b)
129133
}
130134

131135
// Write implements net.Conn's Write interface
132136
func (conn *connWrapper) Write(b []byte) (n int, err error) {
137+
if conn == nil || conn.secureConn == nil {
138+
return 0, io.ErrClosedPipe
139+
}
133140
conn.mtx.Lock()
134141
defer conn.mtx.Unlock()
135142
return conn.secureConn.Write(b)
136143
}
137144

138145
// Close implements net.Conn's Close interface
139146
func (conn *connWrapper) Close() error {
147+
if conn == nil {
148+
return nil
149+
}
140150
conn.mtx.Lock()
141151
defer conn.mtx.Unlock()
142-
conn.secureConn.Close()
143-
return conn.rawConn.Close()
152+
if conn.secureConn != nil {
153+
_ = conn.secureConn.Close()
154+
}
155+
if conn.rawConn != nil {
156+
return conn.rawConn.Close()
157+
}
158+
return nil
144159
}
145160

146161
// LocalAddr implements net.Conn's LocalAddr interface
147162
func (conn *connWrapper) LocalAddr() net.Addr {
163+
if conn == nil || conn.rawConn == nil {
164+
return nil
165+
}
148166
conn.mtx.Lock()
149167
defer conn.mtx.Unlock()
150168
return conn.rawConn.LocalAddr()
151169
}
152170

153171
// RemoteAddr implements net.Conn's RemoteAddr interface
154172
func (conn *connWrapper) RemoteAddr() net.Addr {
173+
if conn == nil || conn.rawConn == nil {
174+
return nil
175+
}
155176
conn.mtx.Lock()
156177
defer conn.mtx.Unlock()
157178
return conn.rawConn.RemoteAddr()
158179
}
159180

160181
// SetDeadline implements net.Conn's SetDeadline interface
161182
func (conn *connWrapper) SetDeadline(t time.Time) error {
183+
if conn == nil || conn.secureConn == nil {
184+
return io.ErrClosedPipe
185+
}
162186
conn.mtx.Lock()
163187
defer conn.mtx.Unlock()
164188
return conn.secureConn.SetDeadline(t)
165189
}
166190

167191
// SetReadDeadline implements net.Conn's SetReadDeadline interface
168192
func (conn *connWrapper) SetReadDeadline(t time.Time) error {
193+
if conn == nil || conn.secureConn == nil {
194+
return io.ErrClosedPipe
195+
}
169196
conn.mtx.Lock()
170197
defer conn.mtx.Unlock()
171198
return conn.secureConn.SetReadDeadline(t)
172199
}
173200

174201
// SetWriteDeadline implements net.Conn's SetWriteDeadline interface
175202
func (conn *connWrapper) SetWriteDeadline(t time.Time) error {
203+
if conn == nil || conn.secureConn == nil {
204+
return io.ErrClosedPipe
205+
}
176206
conn.mtx.Lock()
177207
defer conn.mtx.Unlock()
178208
return conn.secureConn.SetWriteDeadline(t)
@@ -200,6 +230,10 @@ func (pool *ConnPool) Add(addr string, conn net.Conn) {
200230
pool.mtx.Lock()
201231
defer pool.mtx.Unlock()
202232

233+
if w, ok := conn.(*connWrapper); ok && w == nil {
234+
return
235+
}
236+
203237
if item, ok := pool.conns[addr]; ok {
204238
_ = item.conn.Close()
205239
pool.conns[addr] = &connectionItem{lastAccess: time.Now().UTC(), conn: conn}

0 commit comments

Comments
 (0)