From fe3e294c2ab13bb4ce722a3902e68acd4bb5ed33 Mon Sep 17 00:00:00 2001 From: Jackson Owens Date: Tue, 1 Oct 2024 13:33:19 -0400 Subject: [PATCH] colblk: fix panic in Invert Fix a panic where Invert would pass a negative number into slices.Grow if bits at indices higher than `nRows` have been set. --- sstable/colblk/bitmap.go | 5 ++++- sstable/colblk/testdata/bitmap | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sstable/colblk/bitmap.go b/sstable/colblk/bitmap.go index dde6e797e0..3c1b8c640e 100644 --- a/sstable/colblk/bitmap.go +++ b/sstable/colblk/bitmap.go @@ -338,7 +338,10 @@ func (b *BitmapBuilder) Invert(nRows int) { b.minNonZeroRowCount = 1 // If the tail of b is sparse, fill in zeroes before inverting. nBitmapWords := (nRows + 63) >> 6 - b.words = slices.Grow(b.words, nBitmapWords-len(b.words))[:nBitmapWords] + if len(b.words) < nBitmapWords { + b.words = slices.Grow(b.words, nBitmapWords-len(b.words)) + } + b.words = b.words[:nBitmapWords] for i := range b.words { b.words[i] = ^b.words[i] } diff --git a/sstable/colblk/testdata/bitmap b/sstable/colblk/testdata/bitmap index 074984106c..ebe7dff17c 100644 --- a/sstable/colblk/testdata/bitmap +++ b/sstable/colblk/testdata/bitmap @@ -414,3 +414,18 @@ Binary representation: 32-40: b 0000000000011100000000000000000000000000000000000000000000000000 # bitmap word 3 40-48: b 0000000000000000000000000000000000000000000000000000000000000000 # bitmap word 4 48-56: b 0000100000000000000000000000000000000000000000000000000000000000 # bitmap summary word 0-63 + +# Test a case where we invert a bitmap that has more words than we'll need in +# the finished serialized bitmap. Regression test for a bug that caused us to +# pass a negative number into slices.Grow. + +build rows=64 invert +0000000000000000000000000000000000000000000000000000000000000000 +1 +---- +1111111111111111111111111111111111111111111111111111111111111111 +Binary representation: +00-01: x 00 # bitmap encoding +01-08: x 00000000000000 # padding to align to 64-bit boundary +08-16: b 1111111111111111111111111111111111111111111111111111111111111111 # bitmap word 0 +16-24: b 0000000100000000000000000000000000000000000000000000000000000000 # bitmap summary word 0-63