Skip to content

Commit b074967

Browse files
fix: address golangci-lint integer overflow warnings
Add bounds check before converting int to int32 in downscaleExponentialBucket to prevent potential integer overflow issues detected by gosec linter.
1 parent 95c4664 commit b074967

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

exporters/prometheus/exporter.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,21 @@ func downscaleExponentialBucket(bucket metricdata.ExponentialBucket, scaleDelta
258258

259259
// Process each bucket and accumulate counts into the appropriate downscaled bucket
260260
for i := 0; i < len(bucket.Counts); i++ {
261+
// Check if i can be safely converted to int32
262+
if i > math.MaxInt32 {
263+
continue // Skip if index is too large for int32
264+
}
265+
266+
idx := int32(i)
261267
// Calculate the original bucket index, with bounds check to prevent overflow
262268
// Handle both positive and negative offsets
263-
if bucket.Offset > 0 && int32(i) > math.MaxInt32-bucket.Offset {
269+
if bucket.Offset > 0 && idx > math.MaxInt32-bucket.Offset {
264270
continue // Skip buckets that would cause overflow
265271
}
266-
if bucket.Offset < 0 && int32(i) < math.MinInt32-bucket.Offset {
272+
if bucket.Offset < 0 && idx < math.MinInt32-bucket.Offset {
267273
continue // Skip buckets that would cause underflow
268274
}
269-
originalIdx := int32(i) + bucket.Offset
275+
originalIdx := idx + bucket.Offset
270276
// Calculate the downscaled bucket index
271277
downscaledIdx := originalIdx >> scaleDelta
272278
// Accumulate the count

0 commit comments

Comments
 (0)