]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: make skipStdinCopyError a function instead of a variable
authorBryan C. Mills <bcmills@google.com>
Sat, 23 Apr 2022 02:00:16 +0000 (22:00 -0400)
committerBryan Mills <bcmills@google.com>
Tue, 26 Apr 2022 13:47:05 +0000 (13:47 +0000)
This makes clearer that skipStdinCopyError is always defined and never
overridden in tests.

Secondarily, it may also help reduce init-time work and allow the
linker and/or inliner to better optimize this package.

(Noticed while prototyping #50436.)

Change-Id: I4f3c1bc146384a98136a4039f82165ed106c14b8
Reviewed-on: https://go-review.googlesource.com/c/go/+/401897
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/os/exec/exec.go
src/os/exec/exec_plan9.go
src/os/exec/exec_unix.go
src/os/exec/exec_windows.go

index eeca83713bcacad8b9666c05c9aa98bd533cfdf8..91c2e003d8d58931eea1efcbb9c5a24e5e390f11 100644 (file)
@@ -230,10 +230,6 @@ func (c *Cmd) argv() []string {
        return []string{c.Path}
 }
 
-// skipStdinCopyError optionally specifies a function which reports
-// whether the provided stdin copy error should be ignored.
-var skipStdinCopyError func(error) bool
-
 func (c *Cmd) stdin() (f *os.File, err error) {
        if c.Stdin == nil {
                f, err = os.Open(os.DevNull)
@@ -257,7 +253,7 @@ func (c *Cmd) stdin() (f *os.File, err error) {
        c.closeAfterWait = append(c.closeAfterWait, pw)
        c.goroutine = append(c.goroutine, func() error {
                _, err := io.Copy(pw, c.Stdin)
-               if skip := skipStdinCopyError; skip != nil && skip(err) {
+               if skipStdinCopyError(err) {
                        err = nil
                }
                if err1 := pw.Close(); err == nil {
index 21ac7b765f1df0571c21521b4181216ffccf2e8d..8920bec1f5eada8dfba18ae11bea6b25ca842cfe 100644 (file)
@@ -6,14 +6,14 @@ package exec
 
 import "io/fs"
 
-func init() {
-       skipStdinCopyError = func(err error) bool {
-               // Ignore hungup errors copying to stdin if the program
-               // completed successfully otherwise.
-               // See Issue 35753.
-               pe, ok := err.(*fs.PathError)
-               return ok &&
-                       pe.Op == "write" && pe.Path == "|1" &&
-                       pe.Err.Error() == "i/o on hungup channel"
-       }
+// skipStdinCopyError optionally specifies a function which reports
+// whether the provided stdin copy error should be ignored.
+func skipStdinCopyError(err error) bool {
+       // Ignore hungup errors copying to stdin if the program
+       // completed successfully otherwise.
+       // See Issue 35753.
+       pe, ok := err.(*fs.PathError)
+       return ok &&
+               pe.Op == "write" && pe.Path == "|1" &&
+               pe.Err.Error() == "i/o on hungup channel"
 }
index c20f35276c66fb6fc42809aa79aff4a970007cc5..3ed672a7440fbe3948263c04dfaa539a6ca7431a 100644 (file)
@@ -11,14 +11,14 @@ import (
        "syscall"
 )
 
-func init() {
-       skipStdinCopyError = func(err error) bool {
-               // Ignore EPIPE errors copying to stdin if the program
-               // completed successfully otherwise.
-               // See Issue 9173.
-               pe, ok := err.(*fs.PathError)
-               return ok &&
-                       pe.Op == "write" && pe.Path == "|1" &&
-                       pe.Err == syscall.EPIPE
-       }
+// skipStdinCopyError optionally specifies a function which reports
+// whether the provided stdin copy error should be ignored.
+func skipStdinCopyError(err error) bool {
+       // Ignore EPIPE errors copying to stdin if the program
+       // completed successfully otherwise.
+       // See Issue 9173.
+       pe, ok := err.(*fs.PathError)
+       return ok &&
+               pe.Op == "write" && pe.Path == "|1" &&
+               pe.Err == syscall.EPIPE
 }
index bb937f8aed7552228cc020c0d189b5677c2eed9f..e7a2ee6c9db6a7fb8339a76711b643d3c154a355 100644 (file)
@@ -9,15 +9,15 @@ import (
        "syscall"
 )
 
-func init() {
-       skipStdinCopyError = func(err error) bool {
-               // Ignore ERROR_BROKEN_PIPE and ERROR_NO_DATA errors copying
-               // to stdin if the program completed successfully otherwise.
-               // See Issue 20445.
-               const _ERROR_NO_DATA = syscall.Errno(0xe8)
-               pe, ok := err.(*fs.PathError)
-               return ok &&
-                       pe.Op == "write" && pe.Path == "|1" &&
-                       (pe.Err == syscall.ERROR_BROKEN_PIPE || pe.Err == _ERROR_NO_DATA)
-       }
+// skipStdinCopyError optionally specifies a function which reports
+// whether the provided stdin copy error should be ignored.
+func skipStdinCopyError(err error) bool {
+       // Ignore ERROR_BROKEN_PIPE and ERROR_NO_DATA errors copying
+       // to stdin if the program completed successfully otherwise.
+       // See Issue 20445.
+       const _ERROR_NO_DATA = syscall.Errno(0xe8)
+       pe, ok := err.(*fs.PathError)
+       return ok &&
+               pe.Op == "write" && pe.Path == "|1" &&
+               (pe.Err == syscall.ERROR_BROKEN_PIPE || pe.Err == _ERROR_NO_DATA)
 }