Index: CHANGES.txt =================================================================== --- CHANGES.txt (revision 887664) +++ CHANGES.txt (working copy) @@ -32,6 +32,10 @@ one of the threads before all changes are actually committed. (Sanne Grinovero via Mike McCandless) +* LUCENE-2119: Don't throw NegativeArraySizeException if you pass + Integer.MAX_VALUE as nDocs to IndexSearcher search methods. (Paul + Taylor via Mike McCandless) + New features * LUCENE-2069: Added Unicode 4 support to CharArraySet. Due to the switch Index: src/java/org/apache/lucene/search/IndexSearcher.java =================================================================== --- src/java/org/apache/lucene/search/IndexSearcher.java (revision 887664) +++ src/java/org/apache/lucene/search/IndexSearcher.java (working copy) @@ -158,12 +158,14 @@ // inherit javadoc @Override - public TopDocs search(Weight weight, Filter filter, final int nDocs) throws IOException { + public TopDocs search(Weight weight, Filter filter, int nDocs) throws IOException { if (nDocs <= 0) { throw new IllegalArgumentException("nDocs must be > 0"); } + nDocs = Math.min(nDocs, reader.maxDoc()); + TopScoreDocCollector collector = TopScoreDocCollector.create(nDocs, !weight.scoresDocsOutOfOrder()); search(weight, filter, collector); return collector.topDocs(); @@ -186,9 +188,12 @@ * then pass that to {@link #search(Weight, Filter, * Collector)}.

*/ - public TopFieldDocs search(Weight weight, Filter filter, final int nDocs, + public TopFieldDocs search(Weight weight, Filter filter, int nDocs, Sort sort, boolean fillFields) throws IOException { + + nDocs = Math.min(nDocs, reader.maxDoc()); + TopFieldCollector collector = TopFieldCollector.create(sort, nDocs, fillFields, fieldSortDoTrackScores, fieldSortDoMaxScore, !weight.scoresDocsOutOfOrder()); search(weight, filter, collector); Index: src/java/org/apache/lucene/util/PriorityQueue.java =================================================================== --- src/java/org/apache/lucene/util/PriorityQueue.java (revision 887664) +++ src/java/org/apache/lucene/util/PriorityQueue.java (working copy) @@ -85,8 +85,18 @@ if (0 == maxSize) // We allocate 1 extra to avoid if statement in top() heapSize = 2; - else - heapSize = maxSize + 1; + else { + if (maxSize == Integer.MAX_VALUE) { + // Don't wrap heapSize to -1, in this case, which + // causes a confusing NegativeArraySizeException. + // Note that very likely this will simply then hit + // an OOME, but at least that's more indicative to + // caller that this values is too big: + heapSize = Integer.MAX_VALUE; + } else { + heapSize = maxSize + 1; + } + } heap = (T[]) new Object[heapSize]; // T is unbounded type, so this unchecked cast works always this.maxSize = maxSize;