lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject lucene-solr:branch_7x: Fix PriorityQueue constructor to throw IllegalArgumentException if you pass Integer.MAX_VALUE as maxSize (thanks rsaavedraf)
Date Thu, 28 Jun 2018 16:18:23 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x e224f0ed1 -> 154952f47


Fix PriorityQueue constructor to throw IllegalArgumentException if you pass Integer.MAX_VALUE
as maxSize (thanks rsaavedraf)


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/154952f4
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/154952f4
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/154952f4

Branch: refs/heads/branch_7x
Commit: 154952f47d8f4ae23637c42792cc0713c009bde4
Parents: e224f0e
Author: Mike McCandless <mikemccand@apache.org>
Authored: Thu Jun 28 12:11:18 2018 -0400
Committer: Mike McCandless <mikemccand@apache.org>
Committed: Thu Jun 28 12:11:39 2018 -0400

----------------------------------------------------------------------
 .../src/java/org/apache/lucene/util/PriorityQueue.java  | 11 ++++++-----
 .../test/org/apache/lucene/util/TestPriorityQueue.java  | 12 ++++++++++++
 2 files changed, 18 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/154952f4/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java
----------------------------------------------------------------------
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 83ac613..ef08c4a 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<T> implements Iterable<T>
{
       // We allocate 1 extra to avoid if statement in top()
       heapSize = 2;
     } else {
-      // 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) {
+      if ((maxSize < 0) || (maxSize >= ArrayUtil.MAX_ARRAY_LENGTH)) {
         // Throw exception to prevent confusing OOME:
-        throw new IllegalArgumentException("maxSize must be <= " + (ArrayUtil.MAX_ARRAY_LENGTH-1)
+ "; got: " + maxSize);
+        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;
     }
     // T is unbounded type, so this unchecked cast works always:
     @SuppressWarnings("unchecked") final T[] h = (T[]) new Object[heapSize];

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/154952f4/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java
----------------------------------------------------------------------
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 ba28490..efc65d0 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<Boolean>(Integer.MAX_VALUE) {
+          @Override
+          public boolean lessThan(Boolean a, Boolean b) {
+            // uncalled
+            return true;
+          }
+        };
+      });
+  }
 }


Mime
View raw message