Commit c59dc485 authored by Rob Pike's avatar Rob Pike

bytes.Buffer: return error in WriteTo if buffer is not drained

R=rsc
CC=golang-dev
https://golang.org/cl/5642065
parent 3fce00d9
...@@ -182,14 +182,21 @@ func makeSlice(n int) []byte { ...@@ -182,14 +182,21 @@ func makeSlice(n int) []byte {
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) { func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
b.lastRead = opInvalid b.lastRead = opInvalid
if b.off < len(b.buf) { if b.off < len(b.buf) {
nBytes := b.Len()
m, e := w.Write(b.buf[b.off:]) m, e := w.Write(b.buf[b.off:])
if m > nBytes {
panic("bytes.Buffer.WriteTo: invalid Write count")
}
b.off += m b.off += m
n = int64(m) n = int64(m)
if e != nil { if e != nil {
return n, e 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 // Write method in io.Writer
if m != nBytes {
return n, io.ErrShortWrite
}
} }
// Buffer is now empty; reset. // Buffer is now empty; reset.
b.Truncate(0) b.Truncate(0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment