From e2b890f7bacb1e485f74a4cd1fa676fffb806303 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 16 Nov 2024 22:30:59 +0800 Subject: [PATCH] opt: fix the potential memory leaks in pkg/buffer/linkedlist.Buffer Updates #420 --- pkg/buffer/linkedlist/linked_list_buffer.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pkg/buffer/linkedlist/linked_list_buffer.go b/pkg/buffer/linkedlist/linked_list_buffer.go index 0095422ae..8916e9182 100644 --- a/pkg/buffer/linkedlist/linked_list_buffer.go +++ b/pkg/buffer/linkedlist/linked_list_buffer.go @@ -32,7 +32,6 @@ func (b *node) len() int { // Buffer is a linked list of node. type Buffer struct { - bs [][]byte head *node tail *node size int @@ -123,19 +122,19 @@ func (llb *Buffer) Peek(maxBytes int) ([][]byte, error) { } else if maxBytes > llb.Buffered() { return nil, io.ErrShortBuffer } - llb.bs = llb.bs[:0] + var bs [][]byte var cum int for iter := llb.head; iter != nil; iter = iter.next { offset := iter.len() if cum+offset > maxBytes { offset = maxBytes - cum } - llb.bs = append(llb.bs, iter.buf[:offset]) + bs = append(bs, iter.buf[:offset]) if cum += offset; cum == maxBytes { break } } - return llb.bs, nil + return bs, nil } // PeekWithBytes is like Peek but accepts [][]byte and puts them onto head. @@ -145,7 +144,7 @@ func (llb *Buffer) PeekWithBytes(maxBytes int, bs ...[]byte) ([][]byte, error) { } else if maxBytes > llb.Buffered() { return nil, io.ErrShortBuffer } - llb.bs = llb.bs[:0] + var bss [][]byte var cum int for _, b := range bs { if n := len(b); n > 0 { @@ -153,9 +152,9 @@ func (llb *Buffer) PeekWithBytes(maxBytes int, bs ...[]byte) ([][]byte, error) { if cum+offset > maxBytes { offset = maxBytes - cum } - llb.bs = append(llb.bs, b[:offset]) + bss = append(bss, b[:offset]) if cum += offset; cum == maxBytes { - return llb.bs, nil + return bss, nil } } } @@ -164,12 +163,12 @@ func (llb *Buffer) PeekWithBytes(maxBytes int, bs ...[]byte) ([][]byte, error) { if cum+offset > maxBytes { offset = maxBytes - cum } - llb.bs = append(llb.bs, iter.buf[:offset]) + bss = append(bss, iter.buf[:offset]) if cum += offset; cum == maxBytes { break } } - return llb.bs, nil + return bss, nil } // Discard removes some nodes based on n bytes. @@ -266,7 +265,6 @@ func (llb *Buffer) Reset() { llb.tail = nil llb.size = 0 llb.bytes = 0 - llb.bs = nil } // pop returns and removes the head of l. If l is empty, it returns nil.