fix: buffer length panic (#1936)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
(cherry picked from commit eecc6ce574)
This commit is contained in:
Jan-Otto Kröpke
2025-03-14 10:57:47 +01:00
committed by Jan-Otto Kröpke
parent c300935170
commit 9da6e56fcf

View File

@@ -180,7 +180,7 @@ func NewCollectorWithReflection(resultType CounterType, object string, instances
} }
// Get the info with the current buffer size // Get the info with the current buffer size
bufLen := uint32(0) var bufLen uint32
if ret := GetCounterInfo(counterHandle, 0, &bufLen, nil); ret != MoreData { if ret := GetCounterInfo(counterHandle, 0, &bufLen, nil); ret != MoreData {
errs = append(errs, fmt.Errorf("GetCounterInfo: %w", NewPdhError(ret))) errs = append(errs, fmt.Errorf("GetCounterInfo: %w", NewPdhError(ret)))
@@ -188,13 +188,13 @@ func NewCollectorWithReflection(resultType CounterType, object string, instances
continue continue
} }
if bufLen == 0 { buf := make([]byte, bufLen)
if len(buf) == 0 {
errs = append(errs, errors.New("GetCounterInfo: buffer length is zero")) errs = append(errs, errors.New("GetCounterInfo: buffer length is zero"))
continue continue
} }
buf := make([]byte, bufLen)
if ret := GetCounterInfo(counterHandle, 0, &bufLen, &buf[0]); ret != ErrorSuccess { if ret := GetCounterInfo(counterHandle, 0, &bufLen, &buf[0]); ret != ErrorSuccess {
errs = append(errs, fmt.Errorf("GetCounterInfo: %w", NewPdhError(ret))) errs = append(errs, fmt.Errorf("GetCounterInfo: %w", NewPdhError(ret)))