bytedance / gopkg

Universal Utilities for Go
Apache License 2.0
1.71k stars 224 forks source link

mcache.Free(data) panic, when the data is an empty slice #225

Open CGongsz opened 2 months ago

CGongsz commented 2 months ago

Operating System

macos

Go Version

go 1.22.1

Package Version

all version

Affected Packages

protobuf/grpc_compress.go

Expected Behavior

returned normally

Actual Behavior

panic index out of range [-1]

Reproduction Steps

1、Define the empty reponse
message XXXRes { } 2、Write an arbitrary compressor and register it (encoding.RegisterCompressor(compressor)) 3、Triggering the request 4、When the request returns,have a panic,'index out of range [-1]'

Other Information

mcache.Free

func Free(buf []byte) { size := cap(buf) if !isPowerOfTwo(size) { return } buf = buf[:0] caches[bsr(size)].Put(buf) } when size = 0,bsr(size) return -1

caches[-1] = nil

CGongsz commented 2 months ago

The fields in the response body are all default values, 'buf' will always be an empty slice