Skip to content

Commit

Permalink
fix: Handle block offset exceeding chunk length in memchunk.go (grafa…
Browse files Browse the repository at this point in the history
  • Loading branch information
cyriltovena authored and benclive committed Jul 31, 2024
1 parent e4b8a43 commit 57d2199
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkg/chunkenc/memchunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ func newByteChunk(b []byte, blockSize, targetSize int, fromCheckpoint bool) (*Me
blk.uncompressedSize = db.uvarint()
}
l := db.uvarint()
if blk.offset+l > len(b) {
return nil, fmt.Errorf("block %d offset %d + length %d exceeds chunk length %d", i, blk.offset, l, len(b))
}
blk.b = b[blk.offset : blk.offset+l]

// Verify checksums.
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/chunk/client/object_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"strings"
"time"
Expand Down Expand Up @@ -186,7 +187,7 @@ func (o *client) getChunk(ctx context.Context, decodeContext *chunk.DecodeContex
}

if err := c.Decode(decodeContext, buf.Bytes()); err != nil {
return chunk.Chunk{}, errors.WithStack(err)
return chunk.Chunk{}, errors.WithStack(fmt.Errorf("failed to decode chunk '%s': %w", key, err))
}
return c, nil
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/storage/chunk/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/promql"

"github.com/grafana/loki/v3/pkg/logqlmodel/stats"
"github.com/grafana/loki/v3/pkg/storage/chunk"
Expand Down Expand Up @@ -218,8 +217,7 @@ func (c *Fetcher) FetchChunks(ctx context.Context, chunks []chunk.Chunk) ([]chun
}

if err != nil {
// Don't rely on Cortex error translation here.
return nil, promql.ErrStorage{Err: err}
level.Error(log).Log("msg", "failed downloading chunks", "err", err)
}

allChunks := append(fromCache, fromStorage...)
Expand Down

0 comments on commit 57d2199

Please sign in to comment.