]> Cypherpunks repositories - gostls13.git/commitdiff
bytes: panic in ReadFrom with more information with negative Read counts
authorAfanasev Stanislav <phpprogger@gmail.com>
Tue, 3 Oct 2017 19:40:28 +0000 (22:40 +0300)
committerJoe Tsai <thebrokentoaster@gmail.com>
Fri, 6 Oct 2017 06:49:40 +0000 (06:49 +0000)
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>

src/bytes/buffer.go
src/bytes/buffer_test.go

index 099e431a3625736eced984b3dbedcd1241eb00bf..a99e64d1e83c261e8aa1f0a1f072eb1d73cfb557 100644 (file)
@@ -41,6 +41,7 @@ const (
 
 // ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
 var ErrTooLarge = errors.New("bytes.Buffer: too large")
+var errNegativeRead = errors.New("bytes.Buffer: reader returned negative count from Read")
 
 const maxInt = int(^uint(0) >> 1)
 
@@ -198,6 +199,10 @@ func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
        for {
                i := b.grow(MinRead)
                m, e := r.Read(b.buf[i:cap(b.buf)])
+               if m < 0 {
+                       panic(errNegativeRead)
+               }
+
                b.buf = b.buf[:i+m]
                n += int64(m)
                if e == io.EOF {
index 141bbe48216efbe39859ccd3c18df6850e5a5484..e4bbc12f6a13524f6009c805468a5d7574b1a276 100644 (file)
@@ -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 {