Skip to content

Apache Lucene open-source search software

add/improve FST asserts to detect > 2.1 GB FST

Michael McCandless622d6c201d39
diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/FST.java b/lucene/core/src/java/org/apache/lucene/util/fst/FST.java
index 259eb4fe722..dc099696b60 100644
--- a/lucene/core/src/java/org/apache/lucene/util/fst/FST.java
+++ b/lucene/core/src/java/org/apache/lucene/util/fst/FST.java
@@ -669,6 +669,8 @@ public final class FST<T> {
// 2nd pass just "expands" all arcs to take up a fixed
// byte size
final int sizeNeeded = fixedArrayStart + nodeIn.numArcs * maxBytesPerArc;
+ assert ((long) fixedArrayStart) + ((long) nodeIn.numArcs) * maxBytesPerArc < Integer.MAX_VALUE: "FST too large (> 2.1 GB)";
+
bytes = ArrayUtil.grow(bytes, sizeNeeded);
// TODO: we could make this a vInt instead
bytes[fixedArrayStart-4] = (byte) (maxBytesPerArc >> 24);
@@ -685,7 +687,7 @@ public final class FST<T> {
destPos -= maxBytesPerArc;
srcPos -= bytesPerArc[arcIdx];
if (srcPos != destPos) {
- assert destPos > srcPos;
+ assert destPos > srcPos: "destPos=" + destPos + " srcPos=" + srcPos + " arcIdx=" + arcIdx + " maxBytesPerArc=" + maxBytesPerArc + " bytesPerArc[arcIdx]=" + bytesPerArc[arcIdx] + " nodeIn.numArcs=" + nodeIn.numArcs;
System.arraycopy(bytes, srcPos, bytes, destPos, bytesPerArc[arcIdx]);
}
}
@@ -1194,6 +1196,7 @@ public final class FST<T> {
public void writeByte(byte b) {
assert posWrite <= bytes.length;
if (bytes.length == posWrite) {
+ assert bytes.length < Integer.MAX_VALUE: "FST too large (> 2.1 GB)";
bytes = ArrayUtil.grow(bytes);
}
assert posWrite < bytes.length: "posWrite=" + posWrite + " bytes.length=" + bytes.length;
@@ -1203,6 +1206,7 @@ public final class FST<T> {
public void setPosWrite(int posWrite) {
this.posWrite = posWrite;
if (bytes.length < posWrite) {
+ assert bytes.length < Integer.MAX_VALUE: "FST too large (> 2.1 GB)";
bytes = ArrayUtil.grow(bytes, posWrite);
}
}
@@ -1210,6 +1214,7 @@ public final class FST<T> {
@Override
public void writeBytes(byte[] b, int offset, int length) {
final int size = posWrite + length;
+ assert bytes.length < Integer.MAX_VALUE: "FST too large (> 2.1 GB)";
bytes = ArrayUtil.grow(bytes, size);
System.arraycopy(b, offset, bytes, posWrite, length);
posWrite += length;
diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/NodeHash.java b/lucene/core/src/java/org/apache/lucene/util/fst/NodeHash.java
index 73258350ee8..096f2adc927 100644
--- a/lucene/core/src/java/org/apache/lucene/util/fst/NodeHash.java
+++ b/lucene/core/src/java/org/apache/lucene/util/fst/NodeHash.java
@@ -155,6 +155,11 @@ final class NodeHash<T> {
private void rehash() throws IOException {
final int[] oldTable = table;
+
+ if (oldTable.length >= Integer.MAX_VALUE/2) {
+ throw new IllegalStateException("FST too large (> 2.1 GB)");
+ }
+
table = new int[2*table.length];
mask = table.length-1;
for(int idx=0;idx<oldTable.length;idx++) {