From 33c6b2c6a53dde14316fd79457c8281c77873296 Mon Sep 17 00:00:00 2001 From: Ben Ridley Date: Mon, 29 Mar 2021 10:12:20 -0700 Subject: [PATCH] Address GitHub feedback - Defer registry close calls - Ensure size parameter in GetComputerName is properly specified - Clean up some comments to ensure correctness Signed-off-by: Ben Ridley --- collector/os.go | 13 +++++-------- headers/netapi32/netapi32.go | 1 - headers/sysinfoapi/sysinfoapi.go | 4 ++-- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/collector/os.go b/collector/os.go index 5d3f9e42..67e01ec6 100644 --- a/collector/os.go +++ b/collector/os.go @@ -173,6 +173,8 @@ func (c *OSCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) ( // Get total allocation of paging files across all disks. memManKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management`, registry.QUERY_VALUE) + defer memManKey.Close() + if err != nil { return nil, err } @@ -181,15 +183,14 @@ func (c *OSCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) ( return nil, err } - if err := memManKey.Close(); err != nil { - return nil, err - } - // Get build number and product name from registry ntKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) + defer ntKey.Close() + if err != nil { return nil, err } + pn, _, err := ntKey.GetStringValue("ProductName") if err != nil { return nil, err @@ -200,10 +201,6 @@ func (c *OSCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) ( return nil, err } - if err := ntKey.Close(); err != nil { - return nil, err - } - var fsipf float64 = 0 for _, pagingFile := range pagingFiles { fileString := strings.ReplaceAll(pagingFile, `\??\`, "") diff --git a/headers/netapi32/netapi32.go b/headers/netapi32/netapi32.go index 441ccfd2..46b415e6 100644 --- a/headers/netapi32/netapi32.go +++ b/headers/netapi32/netapi32.go @@ -73,7 +73,6 @@ func netApiBufferFree(buffer *wKSTAInfo102) { } // NetWkstaGetInfo returns information about the configuration of a workstation. -// WARNING: The caller must call netApiBufferFree to free the memory allocated by this function. // https://docs.microsoft.com/en-us/windows/win32/api/lmwksta/nf-lmwksta-netwkstagetinfo func netWkstaGetInfo() (*wKSTAInfo102, uint32, error) { var lpwi *wKSTAInfo102 diff --git a/headers/sysinfoapi/sysinfoapi.go b/headers/sysinfoapi/sysinfoapi.go index 7f48c3f7..41471bb9 100644 --- a/headers/sysinfoapi/sysinfoapi.go +++ b/headers/sysinfoapi/sysinfoapi.go @@ -148,12 +148,12 @@ func GetSystemInfo() SystemInfo { } // GetComputerName wraps the GetComputerNameW function in a more Go-like way -// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcomputernamew +// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getcomputernameexw func GetComputerName(f WinComputerNameFormat) (string, error) { // 1kb buffer to accept computer name. This should be more than enough as the maximum size // returned is the max length of a DNS name, which this author believes is 253 characters. size := 1024 - var buffer [4096]uint16 + var buffer [1024]uint16 r1, _, err := procGetComputerNameExW.Call(uintptr(f), uintptr(unsafe.Pointer(&buffer)), uintptr(unsafe.Pointer(&size))) if r1 == 0 { return "", err