Skip to content

Commit 71affe9

Browse files
committed
Fix panic when OnReadCompleted receives negative bytesRead
1 parent a2e9817 commit 71affe9

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

bidirectional_conn.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,16 @@ func (c *BidirectionalConn) Read(p []byte) (n int, err error) {
143143

144144
select {
145145
case bytesRead := <-c.read:
146+
if bytesRead < 0 {
147+
return 0, net.ErrClosed
148+
}
149+
if bytesRead == 0 {
150+
return 0, io.EOF
151+
}
146152
if bytesRead > len(p) {
147153
bytesRead = len(p)
148154
}
149-
if bytesRead > 0 {
150-
copy(p, readBuffer[:bytesRead])
151-
}
155+
copy(p, readBuffer[:bytesRead])
152156
return bytesRead, nil
153157
case <-c.done:
154158
// Wait for Cronet to finish using the buffer before returning.
@@ -364,6 +368,15 @@ func (c *bidirectionalHandler) OnReadCompleted(stream BidirectionalStream, bytes
364368
return
365369
}
366370

371+
if bytesRead < 0 {
372+
c.access.Unlock()
373+
c.signalReadDone()
374+
c.signalWriteDone()
375+
c.cancelOnce.Do(func() {})
376+
c.Close(net.ErrClosed)
377+
return
378+
}
379+
367380
if bytesRead == 0 {
368381
c.access.Unlock()
369382
c.signalReadDone()

0 commit comments

Comments
 (0)