Skip to content

Commit d42476a

Browse files
authored
fix: Handle block offset exceeding chunk length in memchunk.go (#13661)
1 parent 6c4b062 commit d42476a

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

pkg/chunkenc/memchunk.go

+3
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ func newByteChunk(b []byte, blockSize, targetSize int, fromCheckpoint bool) (*Me
476476
blk.uncompressedSize = db.uvarint()
477477
}
478478
l := db.uvarint()
479+
if blk.offset+l > len(b) {
480+
return nil, fmt.Errorf("block %d offset %d + length %d exceeds chunk length %d", i, blk.offset, l, len(b))
481+
}
479482
blk.b = b[blk.offset : blk.offset+l]
480483

481484
// Verify checksums.

pkg/storage/chunk/client/object_client.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/base64"
7+
"fmt"
78
"io"
89
"strings"
910
"time"
@@ -186,7 +187,7 @@ func (o *client) getChunk(ctx context.Context, decodeContext *chunk.DecodeContex
186187
}
187188

188189
if err := c.Decode(decodeContext, buf.Bytes()); err != nil {
189-
return chunk.Chunk{}, errors.WithStack(err)
190+
return chunk.Chunk{}, errors.WithStack(fmt.Errorf("failed to decode chunk '%s': %w", key, err))
190191
}
191192
return c, nil
192193
}

pkg/storage/chunk/fetcher/fetcher.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/opentracing/opentracing-go"
1010
"github.com/prometheus/client_golang/prometheus"
1111
"github.com/prometheus/client_golang/prometheus/promauto"
12-
"github.com/prometheus/prometheus/promql"
1312

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

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

225223
allChunks := append(fromCache, fromStorage...)

0 commit comments

Comments
 (0)