Skip to content

Commit

Permalink
shell_windows: Get pid as uint32 instead int
Browse files Browse the repository at this point in the history
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) {
```
  • Loading branch information
praveenkumar committed Oct 8, 2024
1 parent f3428fe commit 42d1adb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 11 additions & 5 deletions pkg/os/shell/shell_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package shell

import (
"fmt"
"math"
"os"
"path/filepath"
"strings"
Expand All @@ -13,7 +15,7 @@ var (
)

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

for {
if processEntry.ProcessID == uint32(pid) {
if processEntry.ProcessID == pid {
pe = &processEntry
return
}
Expand All @@ -43,21 +45,25 @@ func getProcessEntry(pid int) (pe *syscall.ProcessEntry32, err error) {
}

// getNameAndItsPpid returns the exe file name its parent process id.
func getNameAndItsPpid(pid int) (exefile string, parentid int, err error) {
func getNameAndItsPpid(pid uint32) (exefile string, parentid uint32, err error) {
pe, err := getProcessEntry(pid)
if err != nil {
return "", 0, err
}

name := syscall.UTF16ToString(pe.ExeFile[:])
return name, int(pe.ParentProcessID), nil
return name, pe.ParentProcessID, nil
}

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

if shell == "" {
shell, shellppid, err := getNameAndItsPpid(os.Getppid())
pid := os.Getppid()
if pid < 0 || pid > math.MaxUint32 {
return "", fmt.Errorf("integer overflow for pid: %v", pid)
}
shell, shellppid, err := getNameAndItsPpid(uint32(pid))
if err != nil {
return "cmd", err // defaulting to cmd
}
Expand Down
20 changes: 16 additions & 4 deletions pkg/os/shell/shell_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shell

import (
"math"
"os"
"testing"

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

func TestGetNameAndItsPpidOfCurrent(t *testing.T) {
shell, shellppid, err := getNameAndItsPpid(os.Getpid())

pid := os.Getpid()
if pid < 0 || pid > math.MaxUint32 {
assert.Fail(t, "integer overflow detected")
}
shell, shellppid, err := getNameAndItsPpid(uint32(pid))
assert.Equal(t, "shell.test.exe", shell)
assert.Equal(t, os.Getppid(), shellppid)
ppid := os.Getppid()
if ppid < 0 || ppid > math.MaxUint32 {
assert.Fail(t, "integer overflow detected")
}
assert.Equal(t, uint32(ppid), shellppid)
assert.NoError(t, err)
}

func TestGetNameAndItsPpidOfParent(t *testing.T) {
shell, _, err := getNameAndItsPpid(os.Getppid())
pid := os.Getppid()
if pid < 0 || pid > math.MaxUint32 {
assert.Fail(t, "integer overflow detected")
}
shell, _, err := getNameAndItsPpid(uint32(pid))

assert.Equal(t, "go.exe", shell)
assert.NoError(t, err)
Expand Down

0 comments on commit 42d1adb

Please sign in to comment.