Skip to content

Commit

Permalink
fix(chstorage): check buckets size for overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Aug 23, 2024
1 parent cafb86e commit 7a36d6a
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions internal/chstorage/querier_metrics_hist.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ func (p *expHistIterator) loadValue() (h histogram.Histogram, _ error) {
scale = 8
}

pSpans, pDeltas := convertBucketsLayout(p.values.positiveOffset[p.n], p.values.positiveBucketCounts[p.n], scaleDown)
nSpans, nDeltas := convertBucketsLayout(p.values.negativeOffset[p.n], p.values.negativeBucketCounts[p.n], scaleDown)
pSpans, pDeltas, err := convertBucketsLayout(p.values.positiveOffset[p.n], p.values.positiveBucketCounts[p.n], scaleDown)
if err != nil {
return h, errors.Wrap(err, "convert positive buckets")
}
nSpans, nDeltas, err := convertBucketsLayout(p.values.negativeOffset[p.n], p.values.negativeBucketCounts[p.n], scaleDown)
if err != nil {
return h, errors.Wrap(err, "convert negative buckets")
}

h = histogram.Histogram{
Schema: scale,
Expand Down Expand Up @@ -131,10 +137,13 @@ func (p *expHistIterator) loadValue() (h histogram.Histogram, _ error) {
// to the range (base 1].
//
// scaleDown is the factor by which the buckets are scaled down. In other words 2^scaleDown buckets will be merged into one.
func convertBucketsLayout(offset int32, bucketCounts []uint64, scaleDown int32) (spans []histogram.Span, deltas []int64) {
func convertBucketsLayout(offset int32, bucketCounts []uint64, scaleDown int32) (spans []histogram.Span, deltas []int64, err error) {
numBuckets := len(bucketCounts)
if numBuckets == 0 {
return nil, nil
switch {
case numBuckets == 0:
return nil, nil, nil
case numBuckets > math.MaxInt32:
return nil, nil, errors.Errorf("too much buckets (%d is overflowing int32)", numBuckets)
}

var (
Expand Down Expand Up @@ -206,7 +215,7 @@ func convertBucketsLayout(offset int32, bucketCounts []uint64, scaleDown int32)
}
appendDelta(count)

return spans, deltas
return spans, deltas, nil
}

// At returns the current timestamp/value pair if the value is a float.
Expand Down

0 comments on commit 7a36d6a

Please sign in to comment.