1
0
mirror of https://github.com/golang/go synced 2024-09-28 19:24:29 -06:00

syscall: don't panic when argv is nil on freebsd

The workaround in CL 69970044 introduced a panic when StartProcess is
called with empty argv. Check the length before trying to access it.

Change-Id: Ic948d86c7067a21c484ba24e100d1f1f80179730
Reviewed-on: https://go-review.googlesource.com/c/go/+/500415
Run-TryBot: Roland Shoemaker <roland@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Roland Shoemaker 2023-06-02 08:49:47 -07:00
parent 05293d6b49
commit 96b79bd320
2 changed files with 13 additions and 1 deletions

View File

@ -165,7 +165,7 @@ func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error)
return 0, err
}
if (runtime.GOOS == "freebsd" || runtime.GOOS == "dragonfly") && len(argv[0]) > len(argv0) {
if (runtime.GOOS == "freebsd" || runtime.GOOS == "dragonfly") && len(argv) > 0 && len(argv[0]) > len(argv0) {
argvp[0] = argv0p
}

View File

@ -387,3 +387,15 @@ func TestRlimitRestored(t *testing.T) {
t.Errorf("exec rlimit = %d, want %d", v, orig)
}
}
func TestForkExecNilArgv(t *testing.T) {
defer func() {
if p := recover(); p != nil {
t.Fatal("forkExec panicked")
}
}()
// We don't really care what the result of forkExec is, just that it doesn't
// panic, so we choose something we know won't actually spawn a process (probably).
syscall.ForkExec("/dev/null", nil, nil)
}