From 6b706eb39fbdb33ad0918143e87519679aeeea8c Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 9 Apr 2015 14:12:48 -0400 Subject: [PATCH] stream: fix for large chunks io.Copy() uses a 32*1024 byte chunk size. When a blocksize larger than this was used, the copy was out of bounds. https://gist.github.com/philips/b90ed91068930fe85bed Signed-off-by: Vincent Batts --- stream.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/stream.go b/stream.go index c1f51e7..068bb0c 100644 --- a/stream.go +++ b/stream.go @@ -150,13 +150,22 @@ func (mh *merkleHash) Write(b []byte) (int, error) { offset int ) if mh.lastBlock != nil && mh.lastBlockLen > 0 { + if (mh.lastBlockLen + len(b)) < mh.blockSize { + mh.lastBlockLen += copy(mh.lastBlock[mh.lastBlockLen:], b[:]) + return len(b), nil + } + // XXX off by one? numBytes = copy(curBlock[:], mh.lastBlock[:mh.lastBlockLen]) // not adding to numWritten, since these blocks were accounted for in a // prior Write() // then we'll chunk the front of the incoming bytes - offset = copy(curBlock[numBytes:], b[:(mh.blockSize-numBytes)]) + end := mh.blockSize - numBytes + if end > len(b) { + end = len(b) + } + offset = copy(curBlock[numBytes:], b[:end]) n, err := NewNodeHashBlock(mh.hm, curBlock) if err != nil { // XXX might need to stash again the prior lastBlock and first little chunk