From 85f707b429ecc3d5e2753cae5b082cc3f59aa8f8 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Thu, 24 Aug 2023 13:28:24 +0200 Subject: [PATCH] store: fix error handling in decodePostings --- pkg/store/bucket.go | 5 ++--- pkg/store/bucket_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/store/bucket.go b/pkg/store/bucket.go index f5b71b434ab..780a2013aa4 100644 --- a/pkg/store/bucket.go +++ b/pkg/store/bucket.go @@ -2864,14 +2864,13 @@ func (r *bucketIndexReader) decodeCachedPostings(b []byte) (index.Postings, []fu ) if isDiffVarintSnappyEncodedPostings(b) || isDiffVarintSnappyStreamedEncodedPostings(b) { s := time.Now() - clPostings, err := decodePostings(b) + l, err = decodePostings(b) r.stats.cachedPostingsDecompressions += 1 r.stats.CachedPostingsDecompressionTimeSum += time.Since(s) if err != nil { r.stats.cachedPostingsDecompressionErrors += 1 } else { - closeFns = append(closeFns, clPostings.close) - l = clPostings + closeFns = append(closeFns, l.(closeablePostings).close) } } else { _, l, err = r.dec.Postings(b) diff --git a/pkg/store/bucket_test.go b/pkg/store/bucket_test.go index 0d40f18bc37..31c25d776ab 100644 --- a/pkg/store/bucket_test.go +++ b/pkg/store/bucket_test.go @@ -3330,3 +3330,16 @@ func TestExpandedPostingsRace(t *testing.T) { wg.Wait() } } + +func TestBucketIndexReader_decodeCachedPostingsErrors(t *testing.T) { + bir := bucketIndexReader{stats: &queryStats{}} + t.Run("should return error on broken cached postings without snappy prefix", func(t *testing.T) { + _, _, err := bir.decodeCachedPostings([]byte("foo")) + testutil.NotOk(t, err) + }) + t.Run("should return error on broken cached postings with snappy prefix", func(t *testing.T) { + _, _, err := bir.decodeCachedPostings(append([]byte(codecHeaderSnappy), []byte("foo")...)) + testutil.NotOk(t, err) + }) + +}