]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: don't panic when argv is nil on freebsd
authorRoland Shoemaker <roland@golang.org>
Fri, 2 Jun 2023 15:49:47 +0000 (08:49 -0700)
committerRoland Shoemaker <roland@golang.org>
Mon, 5 Jun 2023 15:22:32 +0000 (15:22 +0000)
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>
src/syscall/exec_unix.go
src/syscall/exec_unix_test.go

index 4b9c04db83ee2c40a8ebab200b29dedd99a0c2a6..14edd023d3ff89d336769d601fd51adc00016da6 100644 (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
        }
 
index 2e5e3df374756c105196ce7676a4b1a3541a2443..9627317bb38a9e373d9aca12c3b8d6e9de3ba1e0 100644 (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)
+}