You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2026/07/03 08:42:20 UTC

(lucene) branch branch_10x updated: Fix integer underflow in assertion in SparseFixedBitSet (#16347)

This is an automated email from the ASF dual-hosted git repository.

romseygeek pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_10x by this push:
     new 343f5359b88 Fix integer underflow in assertion in SparseFixedBitSet (#16347)
343f5359b88 is described below

commit 343f5359b88f8aee0985bde5493db7a98d3b80d2
Author: Alan Woodward <ro...@apache.org>
AuthorDate: Fri Jul 3 09:38:38 2026 +0100

    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.
---
 lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java    | 4 ++--
 .../core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java   | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java b/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java
index ef32a635d8f..6f3c08ced58 100644
--- a/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java
+++ b/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java
@@ -41,12 +41,12 @@ public class SparseFixedBitSet extends BitSet {
   private static final long SINGLE_ELEMENT_ARRAY_BYTES_USED = RamUsageEstimator.sizeOf(new long[1]);
   private static final int MASK_4096 = (1 << 12) - 1;
 
-  private static int blockCount(int length) {
+  static int blockCount(int length) {
     int blockCount = length >>> 12;
     if ((blockCount << 12) < length) {
       ++blockCount;
     }
-    assert (blockCount << 12) >= length;
+    assert ((long) blockCount << 12) >= length;
     return blockCount;
   }
 
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java b/lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java
index 2bdefb1e526..b47ff902088 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java
@@ -155,4 +155,8 @@ public class TestSparseFixedBitSet extends BaseBitSetTestCase<SparseFixedBitSet>
       }
     }
   }
+
+  public void testLargeValuesDoNotOverflow() {
+    assertEquals(524288, SparseFixedBitSet.blockCount(2147479553));
+  }
 }