Check buffer length for datagramBuffer.Read() in case the datagram is larger than the buffer

This commit is contained in:
Andy Wang 2019-08-20 22:50:58 +01:00
parent 46c02d17f4
commit 8b99e419b3
1 changed files with 6 additions and 3 deletions

View File

@ -3,6 +3,7 @@
package multiplex package multiplex
import ( import (
"errors"
"io" "io"
"sync" "sync"
) )
@ -39,9 +40,11 @@ func (d *datagramBuffer) Read(target []byte) (int, error) {
} }
d.rwCond.Wait() d.rwCond.Wait()
} }
// TODO: return error if len(target) is smaller than the datagram data := d.buf[0]
var data []byte if len(target) < len(data) {
data, d.buf = d.buf[0], d.buf[1:] return 0, errors.New("buffer is too small")
}
d.buf = d.buf[1:]
copy(target, data) copy(target, data)
// err will always be nil because we have already verified that buf.Len() != 0 // err will always be nil because we have already verified that buf.Len() != 0
d.rwCond.Broadcast() d.rwCond.Broadcast()