windows: fix EnumProcesses to pass the correct array size

Implementation generated directly with mkwinsyscall has a wrong
assumption about the expected value for PIDs buffer size.

This change adds some small manual code that converts the input
slice length to the number of bytes of the array backing the slice.

A test is also added. It fails with the previous implementation.

Fixes golang/go#60223

Change-Id: I5e2414acb29c6c949e5e6acd328043f8a8883887
Reviewed-on: https://go-review.googlesource.com/c/sys/+/495995
Commit-Queue: Quim Muntal <quimmuntal@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Roman Mazur
2023-05-19 20:19:27 +00:00
committed by Quim Muntal
parent 352d8339e8
commit c8ea6b0cbc
3 changed files with 36 additions and 7 deletions
+22
View File
@@ -717,6 +717,28 @@ func TestWinVerifyTrust(t *testing.T) {
}
func TestEnumProcesses(t *testing.T) {
var (
pids [2]uint32
outSize uint32
)
err := windows.EnumProcesses(pids[:], &outSize)
if err != nil {
t.Fatalf("unable to enumerate processes: %v", err)
}
// Regression check for go.dev/issue/60223
if outSize != 8 {
t.Errorf("unexpected bytes returned: %d", outSize)
}
// Most likely, this should be [0, 4].
// 0 is the system idle pseudo-process. 4 is the initial system process ID.
// This test expects that at least one of the PIDs is not 0.
if pids[0] == 0 && pids[1] == 0 {
t.Errorf("all PIDs are 0")
}
}
func TestProcessModules(t *testing.T) {
process, err := windows.GetCurrentProcess()
if err != nil {