From c7a4099b9926b466b55c7271868d9dfb0271117e Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Fri, 5 Apr 2019 09:03:46 -0700 Subject: [PATCH] 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 --- src/syscall/syscall_darwin.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/syscall/syscall_darwin.go b/src/syscall/syscall_darwin.go index 59669a473dcf7f..7ceceff2c1e727 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