go/src/os/exec
Michael Pratt f659183972 os/exec: avoid NewFile on unknown FDs
exec_test.go's init function uses os.NewFile(fd) + f.Stat as a portable
mechanism to determine if an FD is in use.

Unfortunately, the current use is racy: if an unused FD becomes used
between NewFile and f.Close, then we will unintentionally close an FD we
do not use.

We cannot simply drop Close, as the finalizer will close the FD. We
could hold all of the os.Files in a global for the lifetime of the
process, but the need for such a hack is indicative of the larger
problem: we should not create an os.File for an FD that we do not own.

Instead, the new fdtest.Exists provides a helper that performs the
equivalent of fstat(2) on each OS to determine if the FD is valid,
without using os.File.

We also reuse this helper on a variety of other tests that look at open
FDs.

Fixes #49533

Change-Id: I36e2bdb15f271ab01e55c18db6564271995a15af
Reviewed-on: https://go-review.googlesource.com/c/go/+/364035
Trust: Michael Pratt <mpratt@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-11-16 19:41:37 +00:00
..
internal/fdtest os/exec: avoid NewFile on unknown FDs 2021-11-16 19:41:37 +00:00
bench_test.go
env_test.go
example_test.go all: update references to symbols moved from io/ioutil to io 2020-10-20 18:41:18 +00:00
exec.go all: use bytes.Cut, strings.Cut 2021-10-06 15:53:04 +00:00
exec_linux_test.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
exec_plan9.go all: update references to symbols moved from os to io/fs 2020-10-20 02:32:42 +00:00
exec_posix_test.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
exec_test.go os/exec: avoid NewFile on unknown FDs 2021-11-16 19:41:37 +00:00
exec_unix.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
exec_windows.go all: update references to symbols moved from os to io/fs 2020-10-20 02:32:42 +00:00
exec_windows_test.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
internal_test.go
lp_js.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
lp_plan9.go all: update references to symbols moved from os to io/fs 2020-10-20 02:32:42 +00:00
lp_test.go
lp_unix.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
lp_unix_test.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
lp_windows.go all: update references to symbols moved from os to io/fs 2020-10-20 02:32:42 +00:00
lp_windows_test.go os/exec: re-enable LookPathTest/16 2021-08-12 15:58:41 +00:00
read3.go os/exec: avoid NewFile on unknown FDs 2021-11-16 19:41:37 +00:00