From 35ba05ee288c8760ab116a773b1055a93a419bc5 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 20 Jan 2012 15:39:14 -0800 Subject: [PATCH] bytes: simplified logic Also: Avoid potential crash due to reslicing of nil buffer. R=r CC=golang-dev https://golang.org/cl/5556075 --- src/pkg/bytes/buffer.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/pkg/bytes/buffer.go b/src/pkg/bytes/buffer.go index 9d58326a4fb..ccddd95a49a 100644 --- a/src/pkg/bytes/buffer.go +++ b/src/pkg/bytes/buffer.go @@ -139,21 +139,19 @@ func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) { b.Truncate(0) } for { - if cap(b.buf)-len(b.buf) < MinRead { - var newBuf []byte - // can we get space without allocation? - if b.off+cap(b.buf)-len(b.buf) >= MinRead { - // reuse beginning of buffer - newBuf = b.buf[0 : len(b.buf)-b.off] - } else { - // not enough space at end; put space on end - newBuf = makeSlice(2*(cap(b.buf)-b.off) + MinRead)[:len(b.buf)-b.off] + if free := cap(b.buf) - len(b.buf); free < MinRead { + // not enough space at end + newBuf := b.buf + if b.off+free < MinRead { + // not enough space using beginning of buffer; + // double buffer capacity + newBuf = makeSlice(2*cap(b.buf) + MinRead) if newBuf == nil { return n, ErrTooLarge } } copy(newBuf, b.buf[b.off:]) - b.buf = newBuf + b.buf = newBuf[:len(b.buf)-b.off] b.off = 0 } m, e := r.Read(b.buf[len(b.buf):cap(b.buf)])