From: Keith Randall Date: Fri, 5 Apr 2019 16:03:46 +0000 (-0700) Subject: syscall: dup the argument to fdopendir X-Git-Tag: go1.13beta1~790 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c7a4099b9926b466b55c7271868d9dfb0271117e;p=gostls13.git syscall: dup the argument to fdopendir 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 Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/syscall/syscall_darwin.go b/src/syscall/syscall_darwin.go index 59669a473d..7ceceff2c1 100644 --- a/src/syscall/syscall_darwin.go +++ b/src/syscall/syscall_darwin.go @@ -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