Apache Solr open-source search software
Fix integer overflow.
Adrien Grand960ce15fc895
diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java b/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.javaindex dff8dfb46ad..f1751986862 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java@@ -96,6 +96,20 @@ abstract class AbstractBlockPackedWriter {++ord;}+ // For testing only+ void addBlockOfZeros() throws IOException {+ checkNotFinished();+ if (off != 0 && off != values.length) {+ throw new IllegalStateException("" + off);+ }+ if (off == values.length) {+ flush();+ }+ Arrays.fill(values, 0);+ off = values.length;+ ord += values.length;+ }+/** Flush all buffered data to disk. This instance is not usable anymore* after this method has been called until {@link #reset(DataOutput)} has* been called. */diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReader.java b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReader.javaindex 937e26484af..a33da95450d 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReader.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReader.java@@ -45,7 +45,7 @@ public final class BlockPackedReader {blockShift = Integer.numberOfTrailingZeros(blockSize);blockMask = blockSize - 1;final int numBlocks = (int) (valueCount / blockSize) + (valueCount % blockSize == 0 ? 0 : 1);- if (numBlocks * blockSize < valueCount) {+ if ((long) numBlocks * blockSize < valueCount) {throw new IllegalArgumentException("valueCount is too large for this block size");}long[] minValues = null;diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.javaindex 237829c656a..27b14dddde9 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java@@ -44,7 +44,7 @@ public final class MonotonicBlockPackedReader {blockShift = Integer.numberOfTrailingZeros(blockSize);blockMask = blockSize - 1;final int numBlocks = (int) (valueCount / blockSize) + (valueCount % blockSize == 0 ? 0 : 1);- if (numBlocks * blockSize < valueCount) {+ if ((long) numBlocks * blockSize < valueCount) {throw new IllegalArgumentException("valueCount is too large for this block size");}minValues = new long[numBlocks];diff --git a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.javaindex 15c936b684c..251d9717cb7 100644--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java@@ -36,11 +36,10 @@ import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.LongsRef;import org.apache.lucene.util.LuceneTestCase;-import org.apache.lucene.util.RamUsageEstimator;import org.apache.lucene.util.LuceneTestCase.Slow;+import org.apache.lucene.util.RamUsageEstimator;import org.apache.lucene.util._TestUtil;import org.apache.lucene.util.packed.PackedInts.Reader;-import org.junit.Ignore;import com.carrotsearch.randomizedtesting.generators.RandomInts;@@ -1020,4 +1019,47 @@ public class TestPackedInts extends LuceneTestCase {}}+ @Nightly+ public void testBlockReaderOverflow() throws IOException {+ final long valueCount = _TestUtil.nextLong(random(), 1L + Integer.MAX_VALUE, (long) Integer.MAX_VALUE * 2);+ final int blockSize = 1 << _TestUtil.nextInt(random(), 20, 22);+ final Directory dir = newDirectory();+ final IndexOutput out = dir.createOutput("out.bin", IOContext.DEFAULT);+ final BlockPackedWriter writer = new BlockPackedWriter(out, blockSize);+ long value = random().nextInt() & 0xFFFFFFFFL;+ long valueOffset = _TestUtil.nextLong(random(), 0, valueCount - 1);+ for (long i = 0; i < valueCount; ) {+ assertEquals(i, writer.ord());+ if ((i & (blockSize - 1)) == 0 && (i + blockSize < valueOffset || i > valueOffset && i + blockSize < valueCount)) {+ writer.addBlockOfZeros();+ i += blockSize;+ } else if (i == valueOffset) {+ writer.add(value);+ ++i;+ } else {+ writer.add(0);+ ++i;+ }+ }+ writer.finish();+ out.close();+ final IndexInput in = dir.openInput("out.bin", IOContext.DEFAULT);+ final BlockPackedReaderIterator it = new BlockPackedReaderIterator(in, PackedInts.VERSION_CURRENT, blockSize, valueCount);+ it.skip(valueOffset);+ assertEquals(value, it.next());+ in.seek(0L);+ final BlockPackedReader reader = new BlockPackedReader(in, PackedInts.VERSION_CURRENT, blockSize, valueCount, random().nextBoolean());+ assertEquals(value, reader.get(valueOffset));+ for (int i = 0; i < 5; ++i) {+ final long offset = _TestUtil.nextLong(random(), 0, valueCount - 1);+ if (offset == valueOffset) {+ assertEquals(value, reader.get(offset));+ } else {+ assertEquals(0, reader.get(offset));+ }+ }+ in.close();+ dir.close();+ }+}