[issue-1357] manually allocating increasing buffer

pull/1358/head
elfrucool 3 years ago
parent dd2b43ba52
commit 91974eca63

@ -11,7 +11,7 @@ import (
"time" "time"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"github.com/shirou/gopsutil/v3/internal/procstats" "github.com/shirou/gopsutil/v3/procstats"
) )
var ( var (

@ -185,30 +185,34 @@ func WalkThreads(proc *SYSTEM_PROCESS_INFORMATION, fn func(t SYSTEM_THREAD_INFOR
// the returned structure has methods to walk through the structure // the returned structure has methods to walk through the structure
func GetSystemProcessInformation() (*SystemProcessInformationWalk, error) { func GetSystemProcessInformation() (*SystemProcessInformationWalk, error) {
var ( var (
oneKb uint32 = 1024 oneKb uint32 = 1024
allocKb uint32 = 1 allocKb uint32 = 1
allocBytes uint32 = allocKb * oneKb buffer []byte
buffer []byte usedBytes uint32
usedBytes uint32
) )
buffer = make([]byte, allocBytes) // iterating instead of calling common.CallWithExpandingBuffer hangs forever
for {
st := common.CallWithExpandingBuffer( var allocBytes uint32 = allocKb * oneKb
func() common.NtStatus { buffer = make([]byte, allocBytes)
return common.NtQuerySystemInformation(
windows.SystemProcessInformation, st := common.NtQuerySystemInformation(
&buffer[0], windows.SystemProcessInformation,
allocBytes, &buffer[0],
&usedBytes, allocBytes,
) &usedBytes,
}, )
&buffer,
&usedBytes, if st == common.NtStatus(windows.STATUS_INFO_LENGTH_MISMATCH) {
) allocKb *= 2
continue
}
if st.IsError() {
return nil, st.Error()
}
if st.IsError() { break
return nil, st.Error()
} }
return &SystemProcessInformationWalk{ return &SystemProcessInformationWalk{
Loading…
Cancel
Save