fix(blobstore): include cap check in alloc tests and reject size <= 0

close #3868

Signed-off-by: yanghonggang <yanghonggang_yewu@cmss.chinamobile.com>
This commit is contained in:
yanghonggang 2025-07-28 11:53:52 +08:00 committed by 梁曟風
parent ad376ea9a2
commit d00e08cc2d
2 changed files with 4 additions and 3 deletions

View File

@ -47,7 +47,7 @@ func init() {
// GetPool returns a sync.Pool that generates bytes slice with the size.
// Return nil if no such pool exists.
func GetPool(size int) *sync.Pool {
if size < 0 || size > maxSize {
if size <= 0 || size > maxSize {
return nil
}
bits := msb(size)

View File

@ -23,13 +23,13 @@ import (
func TestUtilBytespool(t *testing.T) {
run := func(size int) {
buff := bytespool.Alloc(size)
if len(buff) != size {
if len(buff) != size || cap(buff) != size {
t.Fatal(size)
}
bytespool.Zero(buff)
bytespool.Free(buff)
bp := bytespool.AllocPointer(size)
if len(*bp) != size {
if len(*bp) != size || cap(*bp) != size {
t.Fatal(size)
}
bytespool.FreePointer(bp)
@ -54,6 +54,7 @@ func TestUtilBytespool(t *testing.T) {
for bits := range [27]struct{}{} {
run(1 << bits)
}
bytespool.Free(nil)
bytespool.FreePointer(nil)
}