From 259a5d825bc6506b25792f4e352d8774d84a30d8 Mon Sep 17 00:00:00 2001 From: poornas Date: Thu, 5 Sep 2019 11:03:32 -0700 Subject: [PATCH] cache - fix corruption when client prematurely terminates request (#8155) --- cmd/disk-cache-backend.go | 13 ++++++++----- cmd/disk-cache.go | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/disk-cache-backend.go b/cmd/disk-cache-backend.go index ee2ad0c3f..0a31519f7 100644 --- a/cmd/disk-cache-backend.go +++ b/cmd/disk-cache-backend.go @@ -418,18 +418,19 @@ func (c *diskCache) bitrotWriteToCache(ctx context.Context, cachePath string, re bufp := c.pool.Get().(*[]byte) defer c.pool.Put(bufp) + var n int for { - n, err := io.ReadFull(reader, *bufp) - if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF && err != io.ErrClosedPipe { + n, err = io.ReadFull(reader, *bufp) + if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF { return 0, err } - eof := err == io.EOF || err == io.ErrUnexpectedEOF || err == io.ErrClosedPipe + eof := err == io.EOF || err == io.ErrUnexpectedEOF if n == 0 && size != 0 { // Reached EOF, nothing more to be done. break } h.Reset() - if _, err := h.Write((*bufp)[:n]); err != nil { + if _, err = h.Write((*bufp)[:n]); err != nil { return 0, err } hashBytes := h.Sum(nil) @@ -522,7 +523,9 @@ func (c *diskCache) Put(ctx context.Context, bucket, object string, data io.Read if err != nil { return err } - + if actualSize != uint64(n) { + return IncompleteBody{} + } return c.saveMetadata(ctx, bucket, object, metadata, n) } diff --git a/cmd/disk-cache.go b/cmd/disk-cache.go index c060c521b..084335431 100644 --- a/cmd/disk-cache.go +++ b/cmd/disk-cache.go @@ -240,7 +240,7 @@ func (c *cacheObjects) GetObjectNInfo(ctx context.Context, bucket, object string pipeWriter.CloseWithError(putErr) }() cleanupBackend := func() { bkReader.Close() } - cleanupPipe := func() { pipeReader.Close() } + cleanupPipe := func() { pipeWriter.Close() } return NewGetObjectReaderFromReader(teeReader, bkReader.ObjInfo, opts.CheckCopyPrecondFn, cleanupBackend, cleanupPipe) }