Skip to content

Commit

Permalink
netconn.go: Fix panic on zero or negative deadline durations
Browse files Browse the repository at this point in the history
Glad no one ran into this in production.
  • Loading branch information
nhooyr committed Oct 19, 2023
1 parent 9d9c971 commit 25a5ca4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ func TestConn(t *testing.T) {
assert.Equal(t, "read msg", s, string(b))
})

t.Run("netConn/pastDeadline", func(t *testing.T) {
tt, c1, c2 := newConnTest(t, nil, nil)

n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary)
n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageBinary)

n1.SetDeadline(time.Now().Add(-time.Minute))
n2.SetDeadline(time.Now().Add(-time.Minute))

// No panic we're good.
})

t.Run("wsjson", func(t *testing.T) {
tt, c1, c2 := newConnTest(t, nil, nil)

Expand Down
12 changes: 10 additions & 2 deletions netconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ func (nc *netConn) SetWriteDeadline(t time.Time) error {
if t.IsZero() {
nc.writeTimer.Stop()
} else {
nc.writeTimer.Reset(time.Until(t))
dur := time.Until(t)
if dur <= 0 {
dur = 1
}
nc.writeTimer.Reset(dur)
}
return nil
}
Expand All @@ -220,7 +224,11 @@ func (nc *netConn) SetReadDeadline(t time.Time) error {
if t.IsZero() {
nc.readTimer.Stop()
} else {
nc.readTimer.Reset(time.Until(t))
dur := time.Until(t)
if dur <= 0 {
dur = 1
}
nc.readTimer.Reset(dur)
}
return nil
}

0 comments on commit 25a5ca4

Please sign in to comment.