From 82447254c0d3ef4d7a5ed58d370e6b6099bccd28 Mon Sep 17 00:00:00 2001 From: dgrr Date: Tue, 7 Jul 2020 16:37:49 +0200 Subject: [PATCH] Fix #11 --- frame.go | 13 +++++++++---- frame_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/frame.go b/frame.go index 4cef703..e223fca 100644 --- a/frame.go +++ b/frame.go @@ -495,9 +495,14 @@ func (fr *Frame) readFrom(r io.Reader) (int64, error) { if err == nil { // reading the payload fr.op[2] &= 127 // quick fix to prevent overflow - if nn := fr.Len(); (fr.max > 0 && nn > fr.max) || nn > limitLen { + if frameSize := fr.Len(); (fr.max > 0 && frameSize > fr.max) || frameSize > limitLen { err = errLenTooBig - } else if nn > 0 { + } else if frameSize > 0 { // read the payload + nn := int64(frameSize) + if nn < 0 { + panic("uint64 to int64 conversion gave a negative number") + } + isClose := fr.IsClose() if isClose { nn -= 2 @@ -506,8 +511,8 @@ func (fr *Frame) readFrom(r io.Reader) (int64, error) { } } - if err == nil { - if rLen := int64(nn) - int64(cap(fr.b)); rLen > 0 { + if err == nil && nn > 0 { + if rLen := nn - int64(cap(fr.b)); rLen > 0 { fr.b = append(fr.b[:cap(fr.b)], make([]byte, rLen)...) } diff --git a/frame_test.go b/frame_test.go index 82907e7..c60ccca 100644 --- a/frame_test.go +++ b/frame_test.go @@ -12,6 +12,21 @@ var ( hugePacket = []byte{0x81, 126, 0, 252, 182, 94, 215, 80, 169, 88, 21, 155, 191, 28, 138, 68, 178, 44, 170, 219, 130, 133, 85, 166, 58, 147, 227, 86, 70, 176, 19, 210, 89, 220, 34, 155, 41, 22, 253, 72, 204, 255, 114, 37, 226, 180, 45, 102, 239, 178, 111, 33, 88, 192, 211, 248, 146, 132, 97, 230, 107, 155, 60, 230, 156, 114, 247, 95, 247, 124, 57, 5, 172, 104, 140, 156, 245, 201, 126, 165, 106, 4, 92, 168, 20, 90, 50, 179, 33, 126, 165, 195, 225, 43, 65, 177, 17, 165, 30, 147, 38, 24, 65, 148, 44, 241, 53, 200, 182, 137, 232, 56, 201, 227, 189, 215, 16, 110, 211, 219, 221, 103, 154, 165, 246, 186, 27, 28, 80, 82, 39, 201, 58, 11, 249, 194, 59, 230, 69, 166, 185, 62, 3, 15, 249, 56, 175, 174, 200, 33, 104, 106, 127, 222, 84, 140, 85, 209, 138, 181, 90, 168, 0, 250, 15, 75, 10, 3, 30, 161, 153, 150, 60, 7, 56, 218, 140, 189, 121, 23, 102, 40, 183, 242, 84, 37, 166, 219, 117, 99, 219, 0, 88, 228, 55, 113, 112, 158, 26, 26, 107, 97, 247, 138, 7, 19, 86, 138, 17, 4, 168, 44, 120, 19, 89, 179, 237, 167, 198, 71, 208, 154, 12, 149, 236, 90, 37, 39, 111, 180, 173, 11, 30, 209, 93, 226, 148, 122, 198, 26, 97, 60, 61, 190, 227, 151, 60, 2, 119, 174, 123, 76, 107, 253, 78, 61} ) +func TestIssue11(t *testing.T) { + fr := AcquireFrame() + // fr.SetBinary() + fr.SetClose() + fr.SetFin() + fr.setLength(1) + + bf := bytes.NewBuffer(nil) + fr.WriteTo(bf) + bf.Write([]byte("some garbage")) + + fr.Reset() + fr.readFrom(bf) // should panic before the commit fixing this thing +} + func TestReadBufio(t *testing.T) { reader := bufio.NewReader( bytes.NewBuffer(littlePacket),