From c579f6fda7f27f1e0e00cda3e5597a680e865dd1 Mon Sep 17 00:00:00 2001 From: POABOB Date: Thu, 23 Nov 2023 11:08:14 +0800 Subject: [PATCH] fix: Prevent ring index overflow --- core/collection/ring.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/collection/ring.go b/core/collection/ring.go index 8218d60dcb20b..a1eb7fd12f588 100644 --- a/core/collection/ring.go +++ b/core/collection/ring.go @@ -1,6 +1,9 @@ package collection -import "sync" +import ( + "math" + "sync" +) // A Ring can be used as fixed size ring. type Ring struct { @@ -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.