mirror of
https://github.com/apache/lucene.git
synced 2025-02-24 11:16:35 +00:00
LUCENE-7933: validate numBits parameter to LongBitSet ctor
This commit is contained in:
parent
ea76351419
commit
63a0c8d92f
@ -63,6 +63,9 @@ Other
|
||||
* LUCENE-7948, LUCENE-7937: Upgrade randomizedtesting to 2.5.3 (minor fixes
|
||||
in 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 Features
|
||||
|
@ -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!)
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user