Skip to content

Commit 9977c3e

Browse files
committed
Fixed potential divide by zero issue
1 parent 80e64d6 commit 9977c3e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pkg/traceql/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func newBucketSet(exemplars uint32, start, end uint64) *limitedBucketSet {
9393
end /= uint64(time.Millisecond.Nanoseconds()) //nolint: gosec // G115
9494

9595
interval := end - start
96-
bucketWidth := interval / uint64(buckets)
96+
bucketWidth := max(interval/uint64(buckets), 1)
9797

9898
return &limitedBucketSet{
9999
sz: int(buckets),

pkg/traceql/util_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ func TestBucketSetSingleExemplar(t *testing.T) {
116116
assert.True(t, s.addAndTest(tsMilli), "ts=%d should not be added to bucket", 100)
117117
}
118118

119+
func TestBucketSetLargeExemplarsShortRange(t *testing.T) {
120+
// exemplars=10000 → buckets=5000, but the range is only 1 second (1000ms interval).
121+
// Without the guard, bucketWidth=0 causes a divide-by-zero in bucket().
122+
s := newBucketSet(10000, 0, uint64(time.Second.Nanoseconds())) //nolint: gosec // G115
123+
assert.NotPanics(t, func() {
124+
s.addAndTest(500) // 500ms into the range
125+
})
126+
assert.False(t, s.testTotal(), "should not be full after one exemplar")
127+
}
128+
119129
func TestBucketSetZeroExemplars(t *testing.T) {
120130
// exemplars=0 means collection is disabled: testTotal() should always return true
121131
// and no exemplars should ever be accepted.

0 commit comments

Comments
 (0)