1
0
mirror of https://github.com/golang/go synced 2024-11-17 12:54:47 -07:00

io: add ErrBadWriteCount

Change-Id: I4421a0904296a349f8c310092b9c6aaf3a9699d1
This commit is contained in:
Tao Qingyun 2020-10-10 09:37:42 +08:00
parent 5bf70f4ee8
commit 838d6bac72
2 changed files with 34 additions and 2 deletions

View File

@ -413,8 +413,10 @@ func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw < 0 || nr < nw {
err = ErrBadWriteCount
break
nw = 0
if ew == nil {
ew = ErrBadWriteCount
}
}
written += int64(nw)
if ew != nil {

View File

@ -429,3 +429,33 @@ func TestSectionReader_Size(t *testing.T) {
}
}
}
// largeWriter returns an invalid count that is larger than the number
// of bytes requested (issue 39978).
type largeWriter struct {
err error
}
func (w largeWriter) Write(p []byte) (int, error) {
return len(p) + 1, w.err
}
func TestCopyLargeWriter(t *testing.T) {
want := ErrBadWriteCount
rb := new(Buffer)
wb := largeWriter{}
rb.WriteString("hello, world.")
if _, err := Copy(wb, rb); err != want {
t.Errorf("Copy Error: got %v, want %v", err, want)
}
}
func TestCopyLargeWriterError(t *testing.T) {
want := errors.New("largerWriteError")
rb := new(Buffer)
wb := largeWriter{err: want}
rb.WriteString("hello, world.")
if _, err := Copy(wb, rb); err != want {
t.Errorf("Copy Error: got %v, want %v", err, want)
}
}