Skip to content

Commit 343f535

Browse files
committed
Fix integer underflow in assertion in SparseFixedBitSet (#16347)
For values close to Integer.MAX_VALUE, the blockCount calculation in SparseFixedBitSet can result in an assertion tripping. The method checks that the calculated block count will cover the full length of the bitset; if the block count covers the whole integer range, the check will underflow to a negative value, which compares as smaller than the passed-in length. This is fixed by a simple cast to long. Note that the error is only in the assertion; the actual calculated block count is correct and there is no production bug.
1 parent 17cb091 commit 343f535

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public class SparseFixedBitSet extends BitSet {
4141
private static final long SINGLE_ELEMENT_ARRAY_BYTES_USED = RamUsageEstimator.sizeOf(new long[1]);
4242
private static final int MASK_4096 = (1 << 12) - 1;
4343

44-
private static int blockCount(int length) {
44+
static int blockCount(int length) {
4545
int blockCount = length >>> 12;
4646
if ((blockCount << 12) < length) {
4747
++blockCount;
4848
}
49-
assert (blockCount << 12) >= length;
49+
assert ((long) blockCount << 12) >= length;
5050
return blockCount;
5151
}
5252

lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,8 @@ public void testNextClearBit() {
155155
}
156156
}
157157
}
158+
159+
public void testLargeValuesDoNotOverflow() {
160+
assertEquals(524288, SparseFixedBitSet.blockCount(2147479553));
161+
}
158162
}

0 commit comments

Comments
 (0)