diff options
| author | Afanasev Stanislav <phpprogger@gmail.com> | 2017-10-03 22:40:28 +0300 |
|---|---|---|
| committer | Joe Tsai <thebrokentoaster@gmail.com> | 2017-10-06 06:49:40 +0000 |
| commit | 07e36af7d6a34d2d70fa794dda44ad2b087c21e1 (patch) | |
| tree | 3e312e26fcaa456bd16069483cd8897650853e6d /src/bytes/buffer_test.go | |
| parent | 9005b220e495ee260230ccf00e54e85abd6df808 (diff) | |
| download | go-07e36af7d6a34d2d70fa794dda44ad2b087c21e1.tar.xz | |
bytes: panic in ReadFrom with more information with negative Read counts
This is only to aid in human debugging, and for that reason we maintain a panic, and not return an error.
Fixes #22097
Change-Id: If72e4d1e47ec9125ca7bc97d5fe4cedb7f76ae72
Reviewed-on: https://go-review.googlesource.com/67970
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/bytes/buffer_test.go')
| -rw-r--r-- | src/bytes/buffer_test.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/bytes/buffer_test.go b/src/bytes/buffer_test.go index 141bbe4821..e4bbc12f6a 100644 --- a/src/bytes/buffer_test.go +++ b/src/bytes/buffer_test.go @@ -17,6 +17,10 @@ const N = 10000 // make this bigger for a larger (and slower) test var testString string // test data for write tests var testBytes []byte // test data; same as testString but as a slice. +type negativeReader struct{} + +func (r *negativeReader) Read([]byte) (int, error) { return -1, nil } + func init() { testBytes = make([]byte, N) for i := 0; i < N; i++ { @@ -265,6 +269,26 @@ func TestReadFrom(t *testing.T) { } } +func TestReadFromNegativeReader(t *testing.T) { + var b Buffer + defer func() { + switch err := recover().(type) { + case nil: + t.Fatal("bytes.Buffer.ReadFrom didn't panic") + case error: + // this is the error string of errNegativeRead + wantError := "bytes.Buffer: reader returned negative count from Read" + if err.Error() != wantError { + t.Fatalf("recovered panic: got %v, want %v", err.Error(), wantError) + } + default: + t.Fatalf("unexpected panic value: %#v", err) + } + }() + + b.ReadFrom(new(negativeReader)) +} + func TestWriteTo(t *testing.T) { var buf Buffer for i := 3; i < 30; i += 3 { |
