From ad53103aef09c002c41ea34292cfea359857ae5b Mon Sep 17 00:00:00 2001 From: Tao Qingyun Date: Tue, 13 Oct 2020 05:56:48 +0000 Subject: io: add ErrBadWriteCount Fixes #39978 Change-Id: Ib41459861ba9f7cf0bf1fc95b1479c358c4bdbd8 GitHub-Last-Rev: 19cbb1461ca04a8eb64f0c4f354d8fb81a70d4f3 GitHub-Pull-Request: golang/go#39989 Reviewed-on: https://go-review.googlesource.com/c/go/+/240740 Run-TryBot: Robert Griesemer TryBot-Result: Go Bot Trust: Ian Lance Taylor Reviewed-by: Robert Griesemer --- src/io/io_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/io/io_test.go') diff --git a/src/io/io_test.go b/src/io/io_test.go index 170513dcc0..a8399bcac6 100644 --- a/src/io/io_test.go +++ b/src/io/io_test.go @@ -429,3 +429,31 @@ func TestSectionReader_Size(t *testing.T) { } } } + +// largeWriter returns an invalid count that is larger than the number +// of bytes provided (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) + } + + want = errors.New("largeWriterError") + 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) + } +} -- cgit v1.2.1 From 03f181a90ef4de680a666ca86c7988915e892e8c Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 15 Oct 2020 23:32:51 -0400 Subject: io: unexport ErrBadWriteCount It was added in CL 240740 to fix #39978 but without any discussion of the exported API. The error can still be returned to fix the issue, without adding new public API to package io. Also fix the error message to refer to lower-case write like the other errors in the package. Change-Id: I134de5eaf3ac903d73913c5cadcde904c5255d79 Reviewed-on: https://go-review.googlesource.com/c/go/+/262877 Trust: Russ Cox Run-TryBot: Russ Cox Reviewed-by: Emmanuel Odeke TryBot-Result: Go Bot --- src/io/io_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/io/io_test.go') diff --git a/src/io/io_test.go b/src/io/io_test.go index a8399bcac6..5b355e8c55 100644 --- a/src/io/io_test.go +++ b/src/io/io_test.go @@ -441,7 +441,7 @@ func (w largeWriter) Write(p []byte) (int, error) { } func TestCopyLargeWriter(t *testing.T) { - want := ErrBadWriteCount + want := ErrInvalidWrite rb := new(Buffer) wb := largeWriter{} rb.WriteString("hello, world.") -- cgit v1.2.1