Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

BUG FIXED: RoundRobinCounter.Add will cause a out of range crash when index >= len(rrc.Values)

yanyiwueaad4fa3c4ec
diff --git a/go/stats/duration_counter.go b/go/stats/duration_counter.go
index 50a8bce43d..69c8be61dd 100644
--- a/go/stats/duration_counter.go
+++ b/go/stats/duration_counter.go
@@ -23,14 +23,11 @@ func NewRoundRobinCounter(slots int) *RoundRobinCounter {
return &RoundRobinCounter{LastIndex: -1, Values: make([]int64, slots), Counts: make([]int64, slots)}
}
func (rrc *RoundRobinCounter) Add(index int, val int64) {
+ if index >= len(rrc.Values) {
+ return
+ }
for rrc.LastIndex != index {
- rrc.LastIndex++
- if rrc.LastIndex >= len(rrc.Values) {
- if index >= len(rrc.Values) {
- break //just avoid endless loop
- }
- rrc.LastIndex = 0
- }
+ rrc.LastIndex = (rrc.LastIndex + 1) % len(rrc.Values)
rrc.Values[rrc.LastIndex] = 0
rrc.Counts[rrc.LastIndex] = 0
}
diff --git a/go/stats/duration_counter_test.go b/go/stats/duration_counter_test.go
new file mode 100644
index 0000000000..aa9d61c87c
--- /dev/null
+++ b/go/stats/duration_counter_test.go
@@ -0,0 +1,19 @@
+package stats
+
+import "testing"
+
+func TestRobinCounter(t *testing.T) {
+ rrc := NewRoundRobinCounter(60)
+ rrc.Add(0, 1)
+ rrc.Add(50, 2)
+ if rrc.Count() != 2 {
+ t.Fatal()
+ }
+ if rrc.Sum() != 3 {
+ t.Fatal()
+ }
+ /*
+ index out of range
+ */
+ rrc.Add(61, 1)
+}