Skip to content

Commit 9f54880

Browse files
Copilotnomeguy
andcommitted
fix: improve error handling and bounds checking in process.go
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
1 parent eb0389b commit 9f54880

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

util/process.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,16 @@ func getPidByPort(port int) (int, error) {
4040
if exitErr.ExitCode() == 1 {
4141
return 0, nil
4242
}
43-
} else {
44-
return 0, err
4543
}
44+
return 0, err
4645
}
4746

4847
lines := strings.Split(string(output), "\n")
4948
for _, line := range lines {
5049
fields := strings.Fields(line)
5150
if len(fields) > 0 {
5251
if runtime.GOOS == "windows" {
53-
if fields[1] == "0.0.0.0:"+strconv.Itoa(port) {
52+
if len(fields) >= 2 && fields[1] == "0.0.0.0:"+strconv.Itoa(port) {
5453
pid, err := strconv.Atoi(fields[len(fields)-1])
5554
if err != nil {
5655
return 0, err
@@ -89,9 +88,8 @@ func StopOldInstance(port int) error {
8988
err = process.Kill()
9089
if err != nil {
9190
return err
92-
} else {
93-
fmt.Printf("The old instance with pid: %d has been stopped\n", pid)
9491
}
9592

93+
fmt.Printf("The old instance with pid: %d has been stopped\n", pid)
9694
return nil
9795
}

0 commit comments

Comments
 (0)