websocket icon indicating copy to clipboard operation
websocket copied to clipboard

[BUG] fix: len(buf) => cap(buf)

Open BaiZe1998 opened this issue 1 year ago • 0 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Current Behavior

Description

len(buf) must equal to 0 for AvailableBuffer()

// AvailableBuffer returns an empty buffer with b.Available() capacity.
// This buffer is intended to be appended to and
// passed to an immediately succeeding [Writer.Write] call.
// The buffer is only valid until the next write operation on b.
func (b *Writer) AvailableBuffer() []byte {
	return b.buf[b.n:][:0]
}

i think the len(buf) may switch to cap(buf) ?

https://github.com/gorilla/websocket/blob/main/server.go#L208

buf := brw.Writer.AvailableBuffer()

var writeBuf []byte
if u.WriteBufferPool == nil && u.WriteBufferSize == 0 && len(buf) >= maxFrameHeaderSize+256 {
    // Reuse hijacked write buffer as connection buffer.
    writeBuf = buf
}

i commit a fix pr: https://github.com/gorilla/websocket/pull/972

Expected Behavior

No response

Steps To Reproduce

No response

Anything else?

No response

BaiZe1998 avatar Jan 17 '25 16:01 BaiZe1998