Skip to content

Apache Lucene open-source search software

Updating Dense#intoBitSet to take into account the provided bitset if upTo equals to NO_MORE_DOCS (#14882)

Panagiotis Bailis96aaa7e80b49
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 6b9a1796c8c..9c723f0369b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -228,6 +228,9 @@ Bug Fixes
* GITHUB#14838: Make it possible to extend Patience/Seeded knn queries (Tommaso Teofili)
+* GITHUB#14882: Fix for IndexedDISI.Dense#intoBitSetWithinBlock to take into account the provided bitset size
+ and avoid throwing IndexOutOfBoundsException (Panagiotis Bailis)
+
Build
---------------------
* Upgrade forbiddenapis to version 3.9. (Uwe Schindler)
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/IndexedDISI.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/IndexedDISI.java
index 1d64be1a928..eb4f27eda14 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/IndexedDISI.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/IndexedDISI.java
@@ -473,7 +473,7 @@ public final class IndexedDISI extends AbstractDocIdSetIterator {
@Override
public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {
- assert doc >= offset;
+ assert doc >= offset : "offset=" + offset + " doc=" + doc;
while (doc < upTo && method.intoBitSetWithinBlock(this, upTo, bitSet, offset) == false) {
readBlockHeader();
boolean found = method.advanceWithinBlock(this, block);
@@ -719,10 +719,10 @@ public final class IndexedDISI extends AbstractDocIdSetIterator {
if (disi.bitSet == null) {
disi.bitSet = new FixedBitSet(BLOCK_SIZE);
}
-
- int sourceFrom = disi.doc & 0xFFFF;
- int sourceTo = Math.min(upTo - disi.block, BLOCK_SIZE);
int destFrom = disi.doc - offset;
+ int disiTo = Math.min(upTo, bitSet.length());
+ int sourceFrom = disi.doc & 0xFFFF;
+ int sourceTo = Math.min(disiTo - disi.block, BLOCK_SIZE);
long fp = disi.slice.getFilePointer();
disi.slice.seek(fp - Long.BYTES); // seek back a long to include current word (disi.word).
@@ -731,13 +731,13 @@ public final class IndexedDISI extends AbstractDocIdSetIterator {
FixedBitSet.orRange(disi.bitSet, sourceFrom, bitSet, destFrom, sourceTo - sourceFrom);
int blockEnd = disi.block | 0xFFFF;
- if (upTo > blockEnd) {
+ if (disiTo > blockEnd) {
disi.slice.seek(disi.blockEnd);
disi.index += disi.bitSet.cardinality(sourceFrom, sourceTo);
return false;
} else {
disi.slice.seek(fp);
- return advanceWithinBlock(disi, upTo);
+ return advanceWithinBlock(disi, disiTo);
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestIndexedDISI.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestIndexedDISI.java
index 135019fe387..708be3255f0 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestIndexedDISI.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestIndexedDISI.java
@@ -388,6 +388,34 @@ public class TestIndexedDISI extends LuceneTestCase {
}
}
+ public void testDenseBitSizeLessThanBlockSize() throws IOException {
+ final byte denseRankPower = (byte) (random().nextInt(7) + 7);
+ try (Directory dir = newDirectory()) {
+ // initialize a maxDoc that is less than IndexedDISI.BLOCK_SIZE
+ int maxDoc = random().nextInt(4096 * 2, 65536);
+ FixedBitSet set = new FixedBitSet(maxDoc);
+ for (int i = 0; i < maxDoc; i += 2) { // Set every other to ensure dense
+ set.set(i);
+ }
+ int jumpTableEntryCount; // this should always be 0 given that maxDoc < BLOCK_SIZE
+ long length;
+ try (IndexOutput out = dir.createOutput("foo", IOContext.DEFAULT)) {
+ jumpTableEntryCount =
+ IndexedDISI.writeBitSet(
+ new BitSetIterator(set, set.cardinality()), out, denseRankPower);
+ length = out.getFilePointer();
+ assertTrue(
+ "jumpTableEntryCount should be 0 for dense bitsets with size < BLOCK_SIZE",
+ 0 == jumpTableEntryCount);
+ }
+ try (IndexInput in = dir.openInput("foo", IOContext.DEFAULT)) {
+ IndexedDISI disi =
+ new IndexedDISI(in, 0L, length, jumpTableEntryCount, denseRankPower, set.cardinality());
+ set.or(disi); // This would throw IOOB if bitset size is not handled correctly as per #14882
+ }
+ }
+ }
+
public void testIllegalDenseRankPower() throws IOException {
// Legal values