浏览代码

Check buffer before releasing

to avoid `extending out of bound` issue in some cases
hmol233 4 年之前
父节点
当前提交
b977899926
共有 1 个文件被更改,包括 5 次插入2 次删除
  1. 5 2
      common/buf/buffer.go

+ 5 - 2
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
 }