]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: dup the argument to fdopendir
authorKeith Randall <keithr@alum.mit.edu>
Fri, 5 Apr 2019 16:03:46 +0000 (09:03 -0700)
committerKeith Randall <khr@golang.org>
Fri, 5 Apr 2019 16:42:06 +0000 (16:42 +0000)
fdopendir takes ownership of its file descriptor argument.
Getdirentries shouldn't do that, so dup the file descriptor
before passing to fdopendir.

Fixes #31269

Change-Id: Ie36be8fd6c59eb339dcc9f40228d4191fc1e5850
Reviewed-on: https://go-review.googlesource.com/c/go/+/170698
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/syscall/syscall_darwin.go

index 59669a473dcf7fb8b343c209335625ba52a205c0..7ceceff2c1e72753248d246167eb36563c8e62d3 100644 (file)
@@ -368,10 +368,15 @@ func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
 func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
        // Simulate Getdirentries using fdopendir/readdir_r/closedir.
        const ptrSize = unsafe.Sizeof(uintptr(0))
-       d, err := fdopendir(fd)
+       fd2, err := Dup(fd)
        if err != nil {
                return 0, err
        }
+       d, err := fdopendir(fd2)
+       if err != nil {
+               Close(fd2)
+               return 0, err
+       }
        defer closedir(d)
        // We keep the number of records already returned in *basep.
        // It's not the full required semantics, but should handle the case