add/improve FST asserts to detect > 2.1 GB FST

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1362667 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2012-07-17 21:33:36 +00:00
parent c33118ce89
commit 622d6c201d
2 changed files with 11 additions and 1 deletions

View File

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

View File

@ -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++) {