mirror of https://github.com/apache/lucene.git
Fix PriorityQueue constructor to throw IllegalArgumentException if you pass Integer.MAX_VALUE as maxSize (thanks rsaavedraf)
This commit is contained in:
parent
ab666ff9cf
commit
4c646dab3f
|
@ -50,14 +50,15 @@ public abstract class PriorityQueue<T> implements Iterable<T> {
|
|||
// We allocate 1 extra to avoid if statement in top()
|
||||
heapSize = 2;
|
||||
} else {
|
||||
|
||||
if ((maxSize < 0) || (maxSize >= ArrayUtil.MAX_ARRAY_LENGTH)) {
|
||||
// Throw exception to prevent confusing OOME:
|
||||
throw new IllegalArgumentException("maxSize must be >= 0 and < " + (ArrayUtil.MAX_ARRAY_LENGTH) + "; got: " + maxSize);
|
||||
}
|
||||
|
||||
// NOTE: we add +1 because all access to heap is
|
||||
// 1-based not 0-based. heap[0] is unused.
|
||||
heapSize = maxSize + 1;
|
||||
|
||||
if (heapSize > ArrayUtil.MAX_ARRAY_LENGTH) {
|
||||
// Throw exception to prevent confusing OOME:
|
||||
throw new IllegalArgumentException("maxSize must be <= " + (ArrayUtil.MAX_ARRAY_LENGTH-1) + "; got: " + maxSize);
|
||||
}
|
||||
}
|
||||
// T is unbounded type, so this unchecked cast works always:
|
||||
@SuppressWarnings("unchecked") final T[] h = (T[]) new Object[heapSize];
|
||||
|
|
|
@ -252,4 +252,16 @@ public class TestPriorityQueue extends LuceneTestCase {
|
|||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
public void testMaxIntSize() {
|
||||
expectThrows(IllegalArgumentException.class, () -> {
|
||||
new PriorityQueue<Boolean>(Integer.MAX_VALUE) {
|
||||
@Override
|
||||
public boolean lessThan(Boolean a, Boolean b) {
|
||||
// uncalled
|
||||
return true;
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue