Skip to content

Commit

Permalink
transport: use io.ReadFull in recv()
Browse files Browse the repository at this point in the history
Currently, recv() does not check the number of bytes read into the
buffer during its Read() calls. This means that in some cases a partial
read may occur, which will cause the next recv() call to read the packet
length incorrectly. In particular, this may be a huge number and cause a
huge buffer allocation. To fix this, simply use io.ReadFull to make sure
we do not get partial reads.

Related: #34

Signed-off-by: Nick Rosbrook <nr@enr0n.net>
  • Loading branch information
enr0n committed Aug 22, 2021
1 parent 470a5cd commit dc4baea
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vici/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (t *transport) send(pkt *packet) error {
func (t *transport) recv() (*packet, error) {
buf := make([]byte, headerLength)

_, err := t.conn.Read(buf)
_, err := io.ReadFull(t.conn, buf)
if err != nil {
if err == io.EOF {
return nil, err
Expand All @@ -94,7 +94,7 @@ func (t *transport) recv() (*packet, error) {
pl := binary.BigEndian.Uint32(buf)

buf = make([]byte, int(pl))
_, err = t.conn.Read(buf)
_, err = io.ReadFull(t.conn, buf)
if err != nil {
if err == io.EOF {
return nil, err
Expand Down

0 comments on commit dc4baea

Please sign in to comment.