Skip to content

Apache Lucene open-source search software

Fix integer overflow in tests.

Adrien Granda426c6fec396
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene80/TestIndexedDISI.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene80/TestIndexedDISI.java
index b15d2cfa836..800c31d5e5d 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene80/TestIndexedDISI.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene80/TestIndexedDISI.java
@@ -476,8 +476,9 @@ public class TestIndexedDISI extends LuceneTestCase {
private void doTestRandom(Directory dir) throws IOException {
Random random = random();
final int maxStep = TestUtil.nextInt(random, 1, 1 << TestUtil.nextInt(random, 2, 20));
- final int numDocs = TestUtil.nextInt(random, 1, Math.min(100000, Integer.MAX_VALUE / maxStep));
- BitSet docs = new SparseFixedBitSet(numDocs * (maxStep + 1));
+ final int numDocs =
+ TestUtil.nextInt(random, 1, Math.min(100000, (Integer.MAX_VALUE - 1) / maxStep));
+ BitSet docs = new SparseFixedBitSet(numDocs * maxStep + 1);
int lastDoc = -1;
for (int doc = -1, i = 0; i < numDocs; ++i) {
doc += TestUtil.nextInt(random, 1, maxStep);
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestIndexedDISI.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestIndexedDISI.java
index a45f0e6d5ac..3ba3db81565 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestIndexedDISI.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestIndexedDISI.java
@@ -473,8 +473,9 @@ public class TestIndexedDISI extends LuceneTestCase {
private void doTestRandom(Directory dir) throws IOException {
Random random = random();
final int maxStep = TestUtil.nextInt(random, 1, 1 << TestUtil.nextInt(random, 2, 20));
- final int numDocs = TestUtil.nextInt(random, 1, Math.min(100000, Integer.MAX_VALUE / maxStep));
- BitSet docs = new SparseFixedBitSet(numDocs * (maxStep + 1));
+ final int numDocs =
+ TestUtil.nextInt(random, 1, Math.min(100000, (Integer.MAX_VALUE - 1) / maxStep));
+ BitSet docs = new SparseFixedBitSet(numDocs * maxStep + 1);
int lastDoc = -1;
for (int doc = -1, i = 0; i < numDocs; ++i) {
doc += TestUtil.nextInt(random, 1, maxStep);