Skip to content

Commit

Permalink
fix: fix the possible overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuschen committed Mar 18, 2024
1 parent d222f97 commit 3332118
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions prometheus/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,22 @@ func (c *counter) Add(v float64) {
}

ival := uint64(v)
if float64(ival) == v {
atomic.AddUint64(&c.valInt, ival)
return
}

// if the v is an unsigned integer
// and the precise doesn't lose during uint cast and cast it back to float
for v == float64(ival) {
oldUint := atomic.LoadUint64(&c.valInt)
leftNum := math.MaxUint64 - oldUint

if leftNum < ival {
// overflow happens, we need to handle as a float number
break
}
if atomic.CompareAndSwapUint64(&c.valInt, oldUint, oldUint+ival) {
return
}

}
for {
oldBits := atomic.LoadUint64(&c.valBits)
newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
Expand Down

0 comments on commit 3332118

Please sign in to comment.