1
0
mirror of https://github.com/prometheus-community/windows_exporter.git synced 2025-04-18 19:24:05 +03:00

service: fix windows.EnumServicesStatusEx reports buffer too small (#1954)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke 2025-03-27 07:26:51 +01:00 committed by GitHub
parent 4c7df1ccaf
commit 63efa92be7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -366,9 +366,9 @@ func (c *Collector) collectService(ch chan<- prometheus.Metric, serviceName stri
// This is realized by ask Service Manager directly.
func (c *Collector) queryAllServices() ([]windows.ENUM_SERVICE_STATUS_PROCESS, error) {
var (
bytesNeeded uint32
servicesReturned uint32
err error
additionalBytesNeeded uint32
servicesReturned uint32
err error
)
for {
@ -381,7 +381,7 @@ func (c *Collector) queryAllServices() ([]windows.ENUM_SERVICE_STATUS_PROCESS, e
windows.SERVICE_STATE_ALL,
&c.queryAllServicesBuffer[0],
currentBufferSize,
&bytesNeeded,
&additionalBytesNeeded,
&servicesReturned,
nil,
nil,
@ -395,11 +395,14 @@ func (c *Collector) queryAllServices() ([]windows.ENUM_SERVICE_STATUS_PROCESS, e
return nil, err
}
if bytesNeeded <= currentBufferSize {
return nil, fmt.Errorf("windows.EnumServicesStatusEx reports buffer too small (%d), but buffer is large enough (%d)", currentBufferSize, bytesNeeded)
}
/*
Unlike other WIN32 API calls, additionalBytesNeeded is not returning the absolute amount bytes needed,
but the additional bytes needed relative to the cbBufSize parameter.
ref:
https://stackoverflow.com/questions/14756347/when-calling-enumservicesstatusex-twice-i-still-get-eror-more-data-in-c
*/
c.queryAllServicesBuffer = make([]byte, bytesNeeded)
c.queryAllServicesBuffer = make([]byte, currentBufferSize+additionalBytesNeeded)
}
if servicesReturned == 0 {