From ee5fe8ea9f72b2b54ac9ae3f87698de4251f9ad0 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Fri, 8 Jun 2018 10:25:12 +0200 Subject: [PATCH] chunks: fix potential "index out of range" error When binary.Uvarint() fails, the returned number of bytes is less than or equal to zero. Signed-off-by: Simon Pasquier --- chunks/chunks.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chunks/chunks.go b/chunks/chunks.go index 9c80767ff7..795740ff3e 100644 --- a/chunks/chunks.go +++ b/chunks/chunks.go @@ -296,7 +296,7 @@ func newReader(bs []ByteSlice, cs []io.Closer, pool chunkenc.Pool) (*Reader, err } // Verify magic number. if m := binary.BigEndian.Uint32(b.Range(0, 4)); m != MagicChunks { - return nil, fmt.Errorf("invalid magic number %x", m) + return nil, errors.Errorf("invalid magic number %x", m) } } return &cr, nil @@ -357,8 +357,8 @@ func (s *Reader) Chunk(ref uint64) (chunkenc.Chunk, error) { r := b.Range(off, off+binary.MaxVarintLen32) l, n := binary.Uvarint(r) - if n < 0 { - return nil, fmt.Errorf("reading chunk length failed") + if n <= 0 { + return nil, errors.Errorf("reading chunk length failed with %d", n) } r = b.Range(off+n, off+n+int(l))