]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: fix exec_bsd.go to accompany exec_linux.go changes
authorRob Pike <r@golang.org>
Tue, 30 Apr 2013 18:52:15 +0000 (11:52 -0700)
committerRob Pike <r@golang.org>
Tue, 30 Apr 2013 18:52:15 +0000 (11:52 -0700)
exec_plan9.go too.
Those are in CL 8334044

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/9055043

src/pkg/syscall/exec_bsd.go
src/pkg/syscall/exec_plan9.go

index bc644d97a4e1e2cb9a3e8d5f2606f1a58fb3d4b2..5d3d57813e50bc3f32e75dc5bd1576ccf7ced77c 100644 (file)
@@ -39,10 +39,18 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
                i      int
        )
 
+       // guard against side effects of shuffling fds below.
+       // Make sure that nextfd is beyond any currently open files so
+       // that we can't run the risk of overwriting any of them.
        fd := make([]int, len(attr.Files))
+       nextfd = len(attr.Files)
        for i, ufd := range attr.Files {
+               if nextfd < int(ufd) {
+                       nextfd = int(ufd)
+               }
                fd[i] = int(ufd)
        }
+       nextfd++
 
        darwin := runtime.GOOS == "darwin"
 
@@ -131,7 +139,6 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
 
        // Pass 1: look for fd[i] < i and move those up above len(fd)
        // so that pass 2 won't stomp on an fd it needs later.
-       nextfd = int(len(fd))
        if pipe < nextfd {
                _, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
                if err1 != 0 {
index ebd57f3e3a7c254428375caff6951247bb88e576..99ad2f15887f5622b8f213432aad4f2ee7d14f27 100644 (file)
@@ -183,11 +183,18 @@ func forkAndExecInChild(argv0 *byte, argv []*byte, envv []envItem, dir *byte, at
                errbuf   [ERRMAX]byte
        )
 
-       // guard against side effects of shuffling fds below.
+       // Guard against side effects of shuffling fds below.
+       // Make sure that nextfd is beyond any currently open files so
+       // that we can't run the risk of overwriting any of them.
        fd := make([]int, len(attr.Files))
+       nextfd = len(attr.Files)
        for i, ufd := range attr.Files {
+               if nextfd < int(ufd) {
+                       nextfd = int(ufd)
+               }
                fd[i] = int(ufd)
        }
+       nextfd++
 
        if envv != nil {
                clearenv = RFCENVG
@@ -251,7 +258,6 @@ func forkAndExecInChild(argv0 *byte, argv []*byte, envv []envItem, dir *byte, at
 
        // Pass 1: look for fd[i] < i and move those up above len(fd)
        // so that pass 2 won't stomp on an fd it needs later.
-       nextfd = int(len(fd))
        if pipe < nextfd {
                r1, _, _ = RawSyscall(SYS_DUP, uintptr(pipe), uintptr(nextfd), 0)
                if int32(r1) == -1 {