Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't call os.NewFile on unknown FDs #24562

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions helper/wrappedstreams/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,29 @@ import (

// Stdin returns the true stdin of the process.
func Stdin() *os.File {
stdin := os.Stdin
if panicwrap.Wrapped(nil) {
stdin = wrappedStdin
}

stdin, _, _ := fds()
return stdin
}

// Stdout returns the true stdout of the process.
func Stdout() *os.File {
stdout := os.Stdout
if panicwrap.Wrapped(nil) {
stdout = wrappedStdout
}

_, stdout, _ := fds()
return stdout
}

// Stderr returns the true stderr of the process.
func Stderr() *os.File {
stderr := os.Stderr
_, _, stderr := fds()
return stderr
}

func fds() (stdin, stdout, stderr *os.File) {
stdin, stdout, stderr = os.Stdin, os.Stdout, os.Stderr
if panicwrap.Wrapped(nil) {
stderr = wrappedStderr
initPlatform()
stdin, stdout, stderr = wrappedStdin, wrappedStdout, wrappedStderr
}

return stderr
return
}

// These are the wrapped standard streams. These are setup by the
Expand All @@ -45,8 +42,3 @@ var (
wrappedStdout *os.File
wrappedStderr *os.File
)

func init() {
// Initialize the platform-specific code
initPlatform()
}
15 changes: 11 additions & 4 deletions helper/wrappedstreams/streams_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ package wrappedstreams

import (
"os"
"sync"
)

var initOnce sync.Once

func initPlatform() {
// The standard streams are passed in via extra file descriptors.
wrappedStdin = os.NewFile(uintptr(3), "stdin")
wrappedStdout = os.NewFile(uintptr(4), "stdout")
wrappedStderr = os.NewFile(uintptr(5), "stderr")
// These must be initialized lazily, once it's been determined that this is
// a wrapped process.
initOnce.Do(func() {
// The standard streams are passed in via extra file descriptors.
wrappedStdin = os.NewFile(uintptr(3), "stdin")
wrappedStdout = os.NewFile(uintptr(4), "stdout")
wrappedStderr = os.NewFile(uintptr(5), "stderr")
})
}