Skip to content

Apache Lucene open-source search software

LUCENE-10678: Fix potential overflow when computing the partition point on the BKD tree (#1065)

Ignacio Verafe8d11254a8a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 13f2669898a..1d273fee966 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -116,6 +116,9 @@ Bug Fixes
* LUCENE-10673: Improve check of equality for latitudes for spatial3d GeoBoundingBox (ignacio Vera)
+* LUCENE-10678: Fix potential overflow when building a BKD tree with more than 4 billion points. The overflow
+ occurs when computing the partition point. (Ignacio Vera)
+
Build
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
index 6f58944622a..44af1e38b19 100644
--- a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
@@ -2018,7 +2018,7 @@ public class BKDWriter implements Closeable {
// How many leaves will be in the left tree:
final int numLeftLeafNodes = getNumLeftLeafNodes(numLeaves);
// How many points will be in the left tree:
- final long leftCount = numLeftLeafNodes * config.maxPointsInLeafNode;
+ final long leftCount = numLeftLeafNodes * (long) config.maxPointsInLeafNode;
BKDRadixSelector.PathSlice[] slices = new BKDRadixSelector.PathSlice[2];
diff --git a/lucene/core/src/test/org/apache/lucene/util/bkd/Test2BBKDPoints.java b/lucene/core/src/test/org/apache/lucene/util/bkd/Test4BBKDPoints.java
similarity index 92%
rename from lucene/core/src/test/org/apache/lucene/util/bkd/Test2BBKDPoints.java
rename to lucene/core/src/test/org/apache/lucene/util/bkd/Test4BBKDPoints.java
index 28bbf2da0ce..da20f914ad1 100644
--- a/lucene/core/src/test/org/apache/lucene/util/bkd/Test2BBKDPoints.java
+++ b/lucene/core/src/test/org/apache/lucene/util/bkd/Test4BBKDPoints.java
@@ -28,19 +28,19 @@ import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.LuceneTestCase.Monster;
import org.apache.lucene.util.NumericUtils;
-// e.g. run like this: ant test -Dtestcase=Test2BBKDPoints -Dtests.nightly=true -Dtests.verbose=true
+// e.g. run like this: ant test -Dtestcase=Test4BBKDPoints -Dtests.nightly=true -Dtests.verbose=true
// -Dtests.monster=true
//
// or: python -u /l/util/src/python/repeatLuceneTest.py -heap 4g -once -nolog -tmpDir /b/tmp
-// -logDir /l/logs Test2BBKDPoints.test2D -verbose
+// -logDir /l/logs Test4BBKDPoints.test2D -verbose
@TimeoutSuite(millis = Integer.MAX_VALUE) // hopefully ~24 days is long enough ;)
@Monster("takes at least 4 hours and consumes many GB of temp disk space")
-public class Test2BBKDPoints extends LuceneTestCase {
+public class Test4BBKDPoints extends LuceneTestCase {
public void test1D() throws Exception {
- Directory dir = FSDirectory.open(createTempDir("2BBKDPoints1D"));
+ Directory dir = FSDirectory.open(createTempDir("4BBKDPoints1D"));
- final int numDocs = (Integer.MAX_VALUE / 26) + 100;
+ final int numDocs = (Integer.MAX_VALUE / 13) + 100;
BKDWriter w =
new BKDWriter(
@@ -83,9 +83,9 @@ public class Test2BBKDPoints extends LuceneTestCase {
}
public void test2D() throws Exception {
- Directory dir = FSDirectory.open(createTempDir("2BBKDPoints2D"));
+ Directory dir = FSDirectory.open(createTempDir("4BBKDPoints2D"));
- final int numDocs = (Integer.MAX_VALUE / 26) + 100;
+ final int numDocs = (Integer.MAX_VALUE / 13) + 100;
BKDWriter w =
new BKDWriter(