Apache Solr open-source search software
LUCENE-7933: validate numBits parameter to LongBitSet ctor
Mike McCandless63a0c8d92f93
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 577c7f4e2b7..f6a37ecd747 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -63,6 +63,9 @@ Other* LUCENE-7948, LUCENE-7937: Upgrade randomizedtesting to 2.5.3 (minor fixesin test filtering for IDEs). (Mike Sokolov, Dawid Weiss)+* LUCENE-7933: LongBitSet now validates the numBits parameter (Won+ Jonghoon, Mike McCandless)+======================= Lucene 7.0.0 =======================New Featuresdiff --git a/lucene/core/src/java/org/apache/lucene/util/LongBitSet.java b/lucene/core/src/java/org/apache/lucene/util/LongBitSet.javaindex ad8fa641c69..89b47658a1b 100644--- a/lucene/core/src/java/org/apache/lucene/util/LongBitSet.java+++ b/lucene/core/src/java/org/apache/lucene/util/LongBitSet.java@@ -55,9 +55,15 @@ public final class LongBitSet {return new LongBitSet(arr, (long)arr.length << 6);}}++ /** The maximum {@code numBits} supported. */+ public static final long MAX_NUM_BITS = 64 * (long) ArrayUtil.MAX_ARRAY_LENGTH;- /** returns the number of 64 bit words it would take to hold numBits */+ /** Returns the number of 64 bit words it would take to hold numBits */public static int bits2words(long numBits) {+ if (numBits < 0 || numBits > MAX_NUM_BITS) {+ throw new IllegalArgumentException("numBits must be 0 .. " + MAX_NUM_BITS + "; got: " + numBits);+ }return (int)((numBits - 1) >> 6) + 1; // I.e.: get the word-offset of the last bit and add one (make sure to use >> so 0 returns 0!)}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestLongBitSet.java b/lucene/core/src/test/org/apache/lucene/util/TestLongBitSet.javaindex cf4d1a74fe2..f94c97eca28 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestLongBitSet.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestLongBitSet.java@@ -218,7 +218,23 @@ public class TestLongBitSet extends LuceneTestCase {assertEquals(b1.hashCode(), b2.hashCode());}}- }+ }++ public void testTooLarge() {+ Exception e = expectThrows(IllegalArgumentException.class,+ () -> {+ new LongBitSet(LongBitSet.MAX_NUM_BITS + 1);+ });+ assertEquals("numBits must be 0 .. 137438952384; got: 137438952385", e.getMessage());+ }++ public void testNegativeNumBits() {+ Exception e = expectThrows(IllegalArgumentException.class,+ () -> {+ new LongBitSet(-17);+ });+ assertEquals("numBits must be 0 .. 137438952384; got: -17", e.getMessage());+ }public void testSmallBitSets() {// Make sure size 0-10 bit sets are OK:@@ -345,6 +361,6 @@ public class TestLongBitSet extends LuceneTestCase {assertEquals(1 << (32-6), LongBitSet.bits2words(1L << 32));assertEquals((1 << (32-6)) + 1, LongBitSet.bits2words((1L << 32)) + 1);// ...- assertEquals(Integer.MAX_VALUE, LongBitSet.bits2words((1L << 37) - 64));+ assertEquals(2147483631, LongBitSet.bits2words(LongBitSet.MAX_NUM_BITS));}}