From 05ab6ee5234d9c5881e903eb001e29d7bb7b7f35 Mon Sep 17 00:00:00 2001 From: Balint Gabor Date: Mon, 15 Jan 2018 18:22:24 +0100 Subject: [PATCH] swarm/storage: Fix prepareChunks in pyramid chunker We need read in a loop until we receive an EOF or the buffer is full, because not all Reader guarantees that it reads the buffer full in one step --- swarm/storage/pyramid.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/swarm/storage/pyramid.go b/swarm/storage/pyramid.go index 5e160a7fed..a34b73993f 100644 --- a/swarm/storage/pyramid.go +++ b/swarm/storage/pyramid.go @@ -414,14 +414,24 @@ func (self *PyramidChunker) prepareChunks(isAppend bool, chunkLevel [][]*TreeEnt var n int var err error chunkData := make([]byte, self.chunkSize+8) + maxBuf := len(chunkData) + readBytes := 8 if unFinishedChunk != nil { copy(chunkData, unFinishedChunk.SData) - n, err = data.Read(chunkData[8+unFinishedChunk.Size:]) - n += int(unFinishedChunk.Size) - unFinishedChunk = nil - } else { - n, err = data.Read(chunkData[8:]) + readBytes += int(unFinishedChunk.Size) } + for readBytes < maxBuf { + n0, err0 := data.Read(chunkData[readBytes:]) + readBytes += n0 + n += n0 + if err0 != nil { + if err0 != io.EOF || (n0 == 0 && maxBuf == readBytes) || n == 0 || n0 != 0 { + err = err0 + } + break + } + } + unFinishedChunk = nil totalDataSize += n if err != nil {