From 248b7214e3f1c47c208c7f93d37373390da66b5b Mon Sep 17 00:00:00 2001 From: Ben Ridley Date: Fri, 19 Mar 2021 10:12:55 +1100 Subject: [PATCH] Move netapi free back to a defer statement Signed-off-by: Ben Ridley --- headers/netapi32/netapi32.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/headers/netapi32/netapi32.go b/headers/netapi32/netapi32.go index 441ccfd2..a24e7ef0 100644 --- a/headers/netapi32/netapi32.go +++ b/headers/netapi32/netapi32.go @@ -75,17 +75,19 @@ 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) { +func netWkstaGetInfo() (wKSTAInfo102, uint32, error) { var lpwi *wKSTAInfo102 pLevel := uintptr(102) r1, _, _ := procNetWkstaGetInfo.Call(0, pLevel, uintptr(unsafe.Pointer(&lpwi))) + defer netApiBufferFree(lpwi) if ret := *(*uint32)(unsafe.Pointer(&r1)); ret != 0 { - return nil, ret, errors.New(NetApiStatus[ret]) + return wKSTAInfo102{}, ret, errors.New(NetApiStatus[ret]) } - return lpwi, 0, nil + deref := *lpwi + return deref, 0, nil } // GetWorkstationInfo is an idiomatic wrapper for netWkstaGetInfo @@ -103,7 +105,5 @@ func GetWorkstationInfo() (WorkstationInfo, error) { LanRoot: windows.UTF16PtrToString(info.wki102_lanroot), LoggedOnUsers: info.wki102_logged_on_users, } - // Free the memory allocated by netapi - netApiBufferFree(info) return workstationInfo, nil }