From bb8d7a1bb98a8372d62b134dfe65961beb374a37 Mon Sep 17 00:00:00 2001 From: Peter Juhasz Date: Thu, 28 Mar 2024 19:49:31 +0100 Subject: [PATCH] -Fix: use io.ReadFull in NextMessage to handle short reads --- audiosocket.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/audiosocket.go b/audiosocket.go index a3e2bf8..c2d0722 100644 --- a/audiosocket.go +++ b/audiosocket.go @@ -116,13 +116,10 @@ func GetID(r io.Reader) (uuid.UUID, error) { func NextMessage(r io.Reader) (Message, error) { hdr := make([]byte, 3) - n, err := r.Read(hdr) + _, err := io.ReadFull(r, hdr) if err != nil { return nil, errors.Wrap(err, "failed to read header") } - if n != 3 { - return nil, errors.Wrapf(err, "read wrong number of bytes (%d) for header", n) - } payloadLen := binary.BigEndian.Uint16(hdr[1:]) if payloadLen < 1 { @@ -130,13 +127,10 @@ func NextMessage(r io.Reader) (Message, error) { } payload := make([]byte, payloadLen) - n, err = r.Read(payload) + _, err = io.ReadFull(r, payload) if err != nil { return nil, errors.Wrap(err, "failed to read payload") } - if n != int(payloadLen) { - return nil, errors.Wrapf(err, "read wrong number of bytes (%d) for payload", n) - } m := append(hdr, payload...)