Skip to content

Commit

Permalink
fix incorrect arguments when starting a child
Browse files Browse the repository at this point in the history
The previous commit introduced a bug in how arguments are passed to
children. We need to make argv[0] the name of the binary, otherwise
the first argument we pass becomes argv[0].
  • Loading branch information
lmb committed Jan 25, 2021
1 parent cae714b commit 9fd66fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func newOSProcess(executable string, args []string, extraFiles []*os.File, env [
Files: fds,
}

args = append([]string{executable}, args...)
pid, _, err := syscall.StartProcess(executable, args, attr)
if err != nil {
return nil, fmt.Errorf("fork/exec: %s", err)
Expand Down
15 changes: 14 additions & 1 deletion process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"golang.org/x/sys/unix"
)

func TestNewOSProcess(t *testing.T) {
func TestFilesAreNonblocking(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -46,6 +46,19 @@ func TestNewOSProcess(t *testing.T) {
}
}

func TestArgumentsArePassedCorrectly(t *testing.T) {
proc, err := newOSProcess("printf", []string{""}, nil, nil)
if err != nil {
t.Fatal("Can't execute printf:", err)
}

// If the argument handling is wrong we'll call printf without any arguments.
// In that case printf exits non-zero.
if err = proc.Wait(); err != nil {
t.Fatal("printf exited non-zero:", err)
}
}

func isNonblock(tb testing.TB, file *os.File) (nonblocking bool) {
tb.Helper()

Expand Down

0 comments on commit 9fd66fb

Please sign in to comment.