LUCENE-7933: validate numBits parameter to LongBitSet ctor

This commit is contained in:
Mike McCandless 2017-09-01 09:15:20 -04:00
parent ea76351419
commit 63a0c8d92f
3 changed files with 28 additions and 3 deletions

View File

@ -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

View File

@ -56,8 +56,14 @@ public final class LongBitSet {
}
}
/** returns the number of 64 bit words it would take to hold numBits */
/** 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 */
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!)
}

View File

@ -220,6 +220,22 @@ public class TestLongBitSet extends LuceneTestCase {
}
}
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:
for(int numBits=0;numBits<10;numBits++) {
@ -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));
}
}