]> Cypherpunks repositories - gostls13.git/commitdiff
os: ensure File.ReadFrom returns ErrClosed instead of the internal poll.ErrFileClosing
authorAndy Pan <panjf2000@gmail.com>
Tue, 21 Feb 2023 15:45:13 +0000 (23:45 +0800)
committerGopher Robot <gobot@golang.org>
Mon, 27 Feb 2023 06:21:27 +0000 (06:21 +0000)
Fixes #58622

Change-Id: Ibb80296c39614478c75cb6bb04b6d0695cb990d3
Reviewed-on: https://go-review.googlesource.com/c/go/+/469795
Run-TryBot: Andy Pan <panjf2000@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/os/error_posix.go
src/os/export_test.go
src/os/file.go
src/os/readfrom_linux.go

index 5ca2e60e5b4f88acd00df6afc242eb897d4dd951..f709d6e344ac93948666eb68e8537d5d33a65544 100644 (file)
@@ -9,7 +9,7 @@ package os
 import "syscall"
 
 // wrapSyscallError takes an error and a syscall name. If the error is
-// a syscall.Errno, it wraps it in a os.SyscallError using the syscall name.
+// a syscall.Errno, it wraps it in an os.SyscallError using the syscall name.
 func wrapSyscallError(name string, err error) error {
        if _, ok := err.(syscall.Errno); ok {
                err = NewSyscallError(name, err)
index f3cb1a2bef071f9724946268074e15a98a011587..dc7caae267d7041134c6a700885eb117c567de7c 100644 (file)
@@ -11,3 +11,7 @@ var LstatP = &lstat
 var ErrWriteAtInAppendMode = errWriteAtInAppendMode
 var TestingForceReadDirLstat = &testingForceReadDirLstat
 var ErrPatternHasSeparator = errPatternHasSeparator
+
+func init() {
+       checkWrapErr = true
+}
index c41adc7da60c7b1f7721b2b123d9726cc9309f39..776e885aff7e52b629f8688ad849d184ab8009a1 100644 (file)
@@ -353,6 +353,10 @@ func fixCount(n int, err error) (int, error) {
        return n, err
 }
 
+// checkWrapErr is the test hook to enable checking unexpected wrapped errors of poll.ErrFileClosing.
+// It is set to true in the export_test.go for tests (including fuzz tests).
+var checkWrapErr = false
+
 // wrapErr wraps an error that occurred during an operation on an open file.
 // It passes io.EOF through unchanged, otherwise converts
 // poll.ErrFileClosing to ErrClosed and wraps the error in a PathError.
@@ -362,6 +366,8 @@ func (f *File) wrapErr(op string, err error) error {
        }
        if err == poll.ErrFileClosing {
                err = ErrClosed
+       } else if checkWrapErr && errors.Is(err, poll.ErrFileClosing) {
+               panic("unexpected error wrapping poll.ErrFileClosing: " + err.Error())
        }
        return &PathError{Op: op, Path: f.name, Err: err}
 }
index 950a6553a4c18b4499c9ee120aa54bf137435afa..2a81b7abfed94b6a1e862ae2a5e8b78c7f609110 100644 (file)
@@ -51,7 +51,7 @@ func (f *File) spliceToFile(r io.Reader) (written int64, handled bool, err error
                lr.N = remain - written
        }
 
-       return written, handled, NewSyscallError(syscallName, err)
+       return written, handled, wrapSyscallError(syscallName, err)
 }
 
 // getPollFD tries to get the poll.FD from the given io.Reader by expecting
@@ -102,7 +102,7 @@ func (f *File) copyFileRange(r io.Reader) (written int64, handled bool, err erro
        if lr != nil {
                lr.N -= written
        }
-       return written, handled, NewSyscallError("copy_file_range", err)
+       return written, handled, wrapSyscallError("copy_file_range", err)
 }
 
 // tryLimitedReader tries to assert the io.Reader to io.LimitedReader, it returns the io.LimitedReader,