]> Cypherpunks repositories - gostls13.git/commitdiff
bytes.Buffer: return error in WriteTo if buffer is not drained
authorRob Pike <r@golang.org>
Wed, 8 Feb 2012 21:58:40 +0000 (08:58 +1100)
committerRob Pike <r@golang.org>
Wed, 8 Feb 2012 21:58:40 +0000 (08:58 +1100)
R=rsc
CC=golang-dev
https://golang.org/cl/5642065

src/pkg/bytes/buffer.go

index a95c2afd005fe302b8b6e557481d11a2f8e6847d..afdf2205598ffd83357c193f29e7fba70b179be1 100644 (file)
@@ -182,14 +182,21 @@ func makeSlice(n int) []byte {
 func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
        b.lastRead = opInvalid
        if b.off < len(b.buf) {
+               nBytes := b.Len()
                m, e := w.Write(b.buf[b.off:])
+               if m > nBytes {
+                       panic("bytes.Buffer.WriteTo: invalid Write count")
+               }
                b.off += m
                n = int64(m)
                if e != nil {
                        return n, e
                }
-               // otherwise all bytes were written, by definition of
+               // all bytes should have been written, by definition of
                // Write method in io.Writer
+               if m != nBytes {
+                       return n, io.ErrShortWrite
+               }
        }
        // Buffer is now empty; reset.
        b.Truncate(0)