From b977899926da99484ca6b891dc36ec799a7b1d9f Mon Sep 17 00:00:00 2001 From: hmol233 <82594500+hmol233@users.noreply.github.com> Date: Mon, 14 Jun 2021 20:40:17 +0800 Subject: [PATCH 1/3] Check buffer before releasing to avoid `extending out of bound` issue in some cases --- common/buf/buffer.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 9eef9997..7c8f965d 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -27,7 +27,7 @@ type Buffer struct { // New creates a Buffer with 0 length and 2K capacity. func New() *Buffer { return &Buffer{ - v: pool.Get().([]byte), + v: buf, } } @@ -64,7 +64,10 @@ func (b *Buffer) Release() { p := b.v b.v = nil b.Clear() - pool.Put(p) + + if cap(p) >= Size { + pool.Put(p) + } b.UDP = nil } From 3531b95d829978f99ef4a53af27f433076868261 Mon Sep 17 00:00:00 2001 From: hmol233 <82594500+hmol233@users.noreply.github.com> Date: Mon, 14 Jun 2021 20:43:48 +0800 Subject: [PATCH 2/3] ? --- common/buf/buffer.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 7c8f965d..baca8238 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -26,6 +26,13 @@ type Buffer struct { // New creates a Buffer with 0 length and 2K capacity. func New() *Buffer { + buf := pool.Get().([]byte) + if cap(buf) >= Size { + buf = buf[:Size] + } else { + buf = make([]byte, Size) + } + return &Buffer{ v: buf, } From e4bf620795cad98862467f7ed74d76440d6c6e1e Mon Sep 17 00:00:00 2001 From: hmol233 <82594500+hmol233@users.noreply.github.com> Date: Sat, 10 Jul 2021 16:41:23 +0800 Subject: [PATCH 3/3] minor tweak --- common/buf/buffer.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/common/buf/buffer.go b/common/buf/buffer.go index baca8238..4878b0b6 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -24,7 +24,7 @@ type Buffer struct { UDP *net.Destination } -// New creates a Buffer with 0 length and 2K capacity. +// New creates a Buffer with 0 length and 8K capacity. func New() *Buffer { buf := pool.Get().([]byte) if cap(buf) >= Size { @@ -57,8 +57,15 @@ func NewExisted(b []byte) *Buffer { // StackNew creates a new Buffer object on stack. // This method is for buffers that is released in the same function. func StackNew() Buffer { + buf := pool.Get().([]byte) + if cap(buf) >= Size { + buf = buf[:Size] + } else { + buf = make([]byte, Size) + } + return Buffer{ - v: pool.Get().([]byte), + v: buf, } } @@ -72,7 +79,7 @@ func (b *Buffer) Release() { b.v = nil b.Clear() - if cap(p) >= Size { + if cap(p) == Size { pool.Put(p) } b.UDP = nil