From eecc6ce574a398cb93d730b5eb2ee0819946a469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Fri, 14 Mar 2025 10:57:47 +0100 Subject: [PATCH] fix: buffer length panic (#1936) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- internal/pdh/collector.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/internal/pdh/collector.go b/internal/pdh/collector.go index 260ae988..6f7cb8c3 100644 --- a/internal/pdh/collector.go +++ b/internal/pdh/collector.go @@ -177,7 +177,7 @@ func NewCollectorWithReflection(resultType CounterType, object string, instances } // Get the info with the current buffer size - bufLen := uint32(0) + var bufLen uint32 if ret := GetCounterInfo(counterHandle, 0, &bufLen, nil); ret != MoreData { errs = append(errs, fmt.Errorf("GetCounterInfo: %w", NewPdhError(ret))) @@ -185,23 +185,29 @@ func NewCollectorWithReflection(resultType CounterType, object string, instances continue } - if bufLen == 0 { + buf := make([]byte, bufLen) + if len(buf) == 0 { errs = append(errs, errors.New("GetCounterInfo: buffer length is zero")) continue } - buf := make([]byte, bufLen) if ret := GetCounterInfo(counterHandle, 0, &bufLen, &buf[0]); ret != ErrorSuccess { errs = append(errs, fmt.Errorf("GetCounterInfo: %w", NewPdhError(ret))) continue } - ci := (*CounterInfo)(unsafe.Pointer(&buf[0])) - counter.Type = ci.DwType - counter.Desc = windows.UTF16PtrToString(ci.SzExplainText) - counter.Desc = windows.UTF16PtrToString(ci.SzExplainText) + counterInfo := (*CounterInfo)(unsafe.Pointer(&buf[0])) + if counterInfo == nil { + errs = append(errs, errors.New("GetCounterInfo: counter info is nil")) + + continue + } + + counter.Type = counterInfo.DwType + counter.Desc = windows.UTF16PtrToString(counterInfo.SzExplainText) + counter.Desc = windows.UTF16PtrToString(counterInfo.SzExplainText) if val, ok := SupportedCounterTypes[counter.Type]; ok { counter.MetricType = val