Skip to content

Commit 42d1adb

Browse files
committed
shell_windows: Get pid as uint32 instead int
os.Getppid() return int value but syscall.ProcessEntry32 struct have `ProcessID` as uint32 so we are doing the type conversion as part of `os.Getppid()` instead later in stage. It will fix following golint error. ``` pkg/os/shell/shell_windows.go:33:38: G115: integer overflow conversion int -> uint32 (gosec) if processEntry.ProcessID == uint32(pid) { ```
1 parent f3428fe commit 42d1adb

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

pkg/os/shell/shell_windows.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package shell
22

33
import (
4+
"fmt"
5+
"math"
46
"os"
57
"path/filepath"
68
"strings"
@@ -13,7 +15,7 @@ var (
1315
)
1416

1517
// re-implementation of private function in https://github.com/golang/go/blob/master/src/syscall/syscall_windows.go
16-
func getProcessEntry(pid int) (pe *syscall.ProcessEntry32, err error) {
18+
func getProcessEntry(pid uint32) (pe *syscall.ProcessEntry32, err error) {
1719
snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
1820
if err != nil {
1921
return nil, err
@@ -30,7 +32,7 @@ func getProcessEntry(pid int) (pe *syscall.ProcessEntry32, err error) {
3032
}
3133

3234
for {
33-
if processEntry.ProcessID == uint32(pid) {
35+
if processEntry.ProcessID == pid {
3436
pe = &processEntry
3537
return
3638
}
@@ -43,21 +45,25 @@ func getProcessEntry(pid int) (pe *syscall.ProcessEntry32, err error) {
4345
}
4446

4547
// getNameAndItsPpid returns the exe file name its parent process id.
46-
func getNameAndItsPpid(pid int) (exefile string, parentid int, err error) {
48+
func getNameAndItsPpid(pid uint32) (exefile string, parentid uint32, err error) {
4749
pe, err := getProcessEntry(pid)
4850
if err != nil {
4951
return "", 0, err
5052
}
5153

5254
name := syscall.UTF16ToString(pe.ExeFile[:])
53-
return name, int(pe.ParentProcessID), nil
55+
return name, pe.ParentProcessID, nil
5456
}
5557

5658
func detect() (string, error) {
5759
shell := os.Getenv("SHELL")
5860

5961
if shell == "" {
60-
shell, shellppid, err := getNameAndItsPpid(os.Getppid())
62+
pid := os.Getppid()
63+
if pid < 0 || pid > math.MaxUint32 {
64+
return "", fmt.Errorf("integer overflow for pid: %v", pid)
65+
}
66+
shell, shellppid, err := getNameAndItsPpid(uint32(pid))
6167
if err != nil {
6268
return "cmd", err // defaulting to cmd
6369
}

pkg/os/shell/shell_windows_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package shell
22

33
import (
4+
"math"
45
"os"
56
"testing"
67

@@ -18,15 +19,26 @@ func TestDetect(t *testing.T) {
1819
}
1920

2021
func TestGetNameAndItsPpidOfCurrent(t *testing.T) {
21-
shell, shellppid, err := getNameAndItsPpid(os.Getpid())
22-
22+
pid := os.Getpid()
23+
if pid < 0 || pid > math.MaxUint32 {
24+
assert.Fail(t, "integer overflow detected")
25+
}
26+
shell, shellppid, err := getNameAndItsPpid(uint32(pid))
2327
assert.Equal(t, "shell.test.exe", shell)
24-
assert.Equal(t, os.Getppid(), shellppid)
28+
ppid := os.Getppid()
29+
if ppid < 0 || ppid > math.MaxUint32 {
30+
assert.Fail(t, "integer overflow detected")
31+
}
32+
assert.Equal(t, uint32(ppid), shellppid)
2533
assert.NoError(t, err)
2634
}
2735

2836
func TestGetNameAndItsPpidOfParent(t *testing.T) {
29-
shell, _, err := getNameAndItsPpid(os.Getppid())
37+
pid := os.Getppid()
38+
if pid < 0 || pid > math.MaxUint32 {
39+
assert.Fail(t, "integer overflow detected")
40+
}
41+
shell, _, err := getNameAndItsPpid(uint32(pid))
3042

3143
assert.Equal(t, "go.exe", shell)
3244
assert.NoError(t, err)

0 commit comments

Comments
 (0)