From 4c646dab3fbf023c0dffd6f4601888760159ed8b Mon Sep 17 00:00:00 2001 From: Mike McCandless Date: Thu, 28 Jun 2018 12:11:18 -0400 Subject: [PATCH] Fix PriorityQueue constructor to throw IllegalArgumentException if you pass Integer.MAX_VALUE as maxSize (thanks rsaavedraf) --- .../java/org/apache/lucene/util/PriorityQueue.java | 11 ++++++----- .../org/apache/lucene/util/TestPriorityQueue.java | 12 ++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java b/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java index 83ac613b676..ef08c4abb1c 100644 --- a/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java +++ b/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java @@ -50,14 +50,15 @@ public abstract class PriorityQueue implements Iterable { // 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]; diff --git a/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java b/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java index ba28490371e..efc65d0d90b 100644 --- a/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java +++ b/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java @@ -252,4 +252,16 @@ public class TestPriorityQueue extends LuceneTestCase { assertEquals(expected, actual); } } + + public void testMaxIntSize() { + expectThrows(IllegalArgumentException.class, () -> { + new PriorityQueue(Integer.MAX_VALUE) { + @Override + public boolean lessThan(Boolean a, Boolean b) { + // uncalled + return true; + } + }; + }); + } }