Skip to content

Commit

Permalink
fix: Handle block offset exceeding chunk length in memchunk.go (#13661)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyriltovena authored Jul 26, 2024
1 parent 6c4b062 commit d42476a
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 d42476a

Please sign in to comment.