Skip to content

Commit

Permalink
fix: Prevent ring index overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
POABOB committed Nov 23, 2023
1 parent 2281aab commit c579f6f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions core/collection/ring.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package collection

import "sync"
import (
"math"
"sync"
)

// A Ring can be used as fixed size ring.
type Ring struct {
Expand All @@ -26,7 +29,7 @@ func (r *Ring) Add(v any) {
defer r.lock.Unlock()

r.elements[r.index%len(r.elements)] = v
r.index++
r.index = (r.index + 1) % math.MaxInt32 // prevent ring index overflow
}

// Take takes all items from r.
Expand Down

0 comments on commit c579f6f

Please sign in to comment.