From bf971e63462a3c433f3feb8e2447fe2d0cd1ca19 Mon Sep 17 00:00:00 2001 From: Abhi Dangeti Date: Wed, 16 Oct 2024 10:50:53 -0600 Subject: [PATCH] Add nil pointer checks for postings before dereference (#271) For https://github.com/blevesearch/bleve/issues/1662 --- merge.go | 4 ++++ section_inverted_text_index.go | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/merge.go b/merge.go index 560490b2..683d9209 100644 --- a/merge.go +++ b/merge.go @@ -304,6 +304,10 @@ func writePostings(postings *roaring.Bitmap, tfEncoder, locEncoder *chunkedIntCo use1HitEncoding func(uint64) (bool, uint64, uint64), w *CountHashWriter, bufMaxVarintLen64 []byte) ( offset uint64, err error) { + if postings == nil { + return 0, nil + } + termCardinality := postings.GetCardinality() if termCardinality <= 0 { return 0, nil diff --git a/section_inverted_text_index.go b/section_inverted_text_index.go index d19dc945..91a126e5 100644 --- a/section_inverted_text_index.go +++ b/section_inverted_text_index.go @@ -412,7 +412,6 @@ func (i *invertedIndexOpaque) BytesRead() uint64 { func (i *invertedIndexOpaque) ResetBytesRead(uint64) {} func (io *invertedIndexOpaque) writeDicts(w *CountHashWriter) (dictOffsets []uint64, err error) { - if io.results == nil || len(io.results) == 0 { return nil, nil } @@ -462,7 +461,11 @@ func (io *invertedIndexOpaque) writeDicts(w *CountHashWriter) (dictOffsets []uin locs := io.Locs[pid] locOffset := 0 - chunkSize, err := getChunkSize(io.chunkMode, postingsBS.GetCardinality(), uint64(len(io.results))) + var cardinality uint64 + if postingsBS != nil { + cardinality = postingsBS.GetCardinality() + } + chunkSize, err := getChunkSize(io.chunkMode, cardinality, uint64(len(io.results))) if err != nil { return nil, err }