]> Cypherpunks.ru repositories - gostls13.git/commit
os/exec: avoid NewFile on unknown FDs
authorMichael Pratt <mpratt@google.com>
Mon, 15 Nov 2021 18:50:39 +0000 (13:50 -0500)
committerMichael Pratt <mpratt@google.com>
Tue, 16 Nov 2021 19:41:37 +0000 (19:41 +0000)
commitf6591839727e09cc5cb11d08b333fd2386e8aa1b
tree2f68f68572c9f69c42c59bb01580f855a91516a1
parent6c36c332fefdd433cfe6e6468a2542fc310e9f8a
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>
src/go/build/deps_test.go
src/os/exec/exec_test.go
src/os/exec/internal/fdtest/exists_js.go [new file with mode: 0644]
src/os/exec/internal/fdtest/exists_plan9.go [new file with mode: 0644]
src/os/exec/internal/fdtest/exists_test.go [new file with mode: 0644]
src/os/exec/internal/fdtest/exists_unix.go [new file with mode: 0644]
src/os/exec/internal/fdtest/exists_windows.go [new file with mode: 0644]
src/os/exec/read3.go