From 93886d84172503c30a58c60a90d0f3be3a9903c0 Mon Sep 17 00:00:00 2001 From: Callum Styan Date: Thu, 2 Sep 2021 11:08:05 -0700 Subject: [PATCH] Fix div by 0 panic is resize function. (#9286) Signed-off-by: Callum Styan --- tsdb/exemplar.go | 2 +- tsdb/exemplar_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tsdb/exemplar.go b/tsdb/exemplar.go index c7cefaba0..1e09da21e 100644 --- a/tsdb/exemplar.go +++ b/tsdb/exemplar.go @@ -279,7 +279,7 @@ func (ce *CircularExemplarStorage) Resize(l int64) int { migrated := 0 - if l > 0 { + if l > 0 && len(oldBuffer) > 0 { // Rewind previous next index by count with wrap-around. // This math is essentially looking at nextIndex, where we would write the next exemplar to, // and find the index in the old exemplar buffer that we should start migrating exemplars from. diff --git a/tsdb/exemplar_test.go b/tsdb/exemplar_test.go index 8fcaf6d2e..eb95daa34 100644 --- a/tsdb/exemplar_test.go +++ b/tsdb/exemplar_test.go @@ -413,7 +413,7 @@ func TestResize(t *testing.T) { expectedMigrated: 50, }, { - name: "Zero", + name: "ShrinkToZero", startSize: 100, newCount: 0, expectedSeries: []int{}, @@ -436,6 +436,14 @@ func TestResize(t *testing.T) { notExpectedSeries: []int{}, expectedMigrated: 0, }, + { + name: "GrowFromZero", + startSize: 0, + newCount: 10, + expectedSeries: []int{}, + notExpectedSeries: []int{}, + expectedMigrated: 0, + }, } for _, tc := range testCases {