Skip to content

Commit 25a5ca4

Browse files
committed
netconn.go: Fix panic on zero or negative deadline durations
Glad no one ran into this in production.
1 parent 9d9c971 commit 25a5ca4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

conn_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ func TestConn(t *testing.T) {
236236
assert.Equal(t, "read msg", s, string(b))
237237
})
238238

239+
t.Run("netConn/pastDeadline", func(t *testing.T) {
240+
tt, c1, c2 := newConnTest(t, nil, nil)
241+
242+
n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary)
243+
n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageBinary)
244+
245+
n1.SetDeadline(time.Now().Add(-time.Minute))
246+
n2.SetDeadline(time.Now().Add(-time.Minute))
247+
248+
// No panic we're good.
249+
})
250+
239251
t.Run("wsjson", func(t *testing.T) {
240252
tt, c1, c2 := newConnTest(t, nil, nil)
241253

netconn.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ func (nc *netConn) SetWriteDeadline(t time.Time) error {
210210
if t.IsZero() {
211211
nc.writeTimer.Stop()
212212
} else {
213-
nc.writeTimer.Reset(time.Until(t))
213+
dur := time.Until(t)
214+
if dur <= 0 {
215+
dur = 1
216+
}
217+
nc.writeTimer.Reset(dur)
214218
}
215219
return nil
216220
}
@@ -220,7 +224,11 @@ func (nc *netConn) SetReadDeadline(t time.Time) error {
220224
if t.IsZero() {
221225
nc.readTimer.Stop()
222226
} else {
223-
nc.readTimer.Reset(time.Until(t))
227+
dur := time.Until(t)
228+
if dur <= 0 {
229+
dur = 1
230+
}
231+
nc.readTimer.Reset(dur)
224232
}
225233
return nil
226234
}

0 commit comments

Comments
 (0)