Skip to content

Commit 8c5a93f

Browse files
author
Stefan Vodita
committed
Throw exception when minLength > maxLength even if minLength >= array.length
1 parent 285a3ba commit 8c5a93f

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,6 @@ public static int[] growExact(int[] array, int newLength) {
337337
public static int[] growInRange(int[] array, int minLength, int maxLength) {
338338
assert minLength >= 0
339339
: "length must be positive (got " + minLength + "): likely integer overflow?";
340-
if (array.length >= minLength) {
341-
return array;
342-
}
343340

344341
if (minLength > maxLength) {
345342
throw new IllegalArgumentException(
@@ -349,6 +346,10 @@ public static int[] growInRange(int[] array, int minLength, int maxLength) {
349346
+ maxLength);
350347
}
351348

349+
if (array.length >= minLength) {
350+
return array;
351+
}
352+
352353
int potentialLength = oversize(minLength, Integer.BYTES);
353354
return growExact(array, Math.min(maxLength, potentialLength));
354355
}

lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,18 +376,21 @@ public void testGrowExact() {
376376
public void testGrowInRange() {
377377
int[] array = new int[] {1, 2, 3};
378378

379-
// maxLength does not matter as long as minLength is enough
380-
assertSame(array, growInRange(array, 1, 4));
381-
assertSame(array, growInRange(array, 1, 4));
382-
assertSame(array, growInRange(array, 1, 2));
383-
assertSame(array, growInRange(array, 1, 1));
384-
assertSame(array, growInRange(array, 1, 0));
385-
assertSame(array, growInRange(array, 1, -1));
379+
// If minLength is negative, maxLength does not matter
380+
expectThrows(AssertionError.class, () -> growInRange(array, -1, 4));
381+
expectThrows(AssertionError.class, () -> growInRange(array, -1, 0));
382+
expectThrows(AssertionError.class, () -> growInRange(array, -1, -1));
386383

387-
// maxLength < minLength if the array has to be grown throws an exception
384+
// If minLength > maxLength, we throw an exception
385+
expectThrows(IllegalArgumentException.class, () -> growInRange(array, 1, 0));
388386
expectThrows(IllegalArgumentException.class, () -> growInRange(array, 4, 3));
389387
expectThrows(IllegalArgumentException.class, () -> growInRange(array, 5, 4));
390388

389+
// If minLength is sufficient, we return the array
390+
assertSame(array, growInRange(array, 1, 4));
391+
assertSame(array, growInRange(array, 1, 2));
392+
assertSame(array, growInRange(array, 1, 1));
393+
391394
int minLength = 4;
392395
int maxLength = Integer.MAX_VALUE;
393396

lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ public int[] getBulkOrdinals(FacetLabel... categoryPaths) throws IOException {
327327
}
328328
// First try to find results in the cache:
329329
int[] result = new int[categoryPaths.length];
330-
int[] indexesMissingFromCache = new int[10]; // initial size, will grow when required
330+
// Will grow when required, but never beyond categoryPaths.length
331+
int[] indexesMissingFromCache = new int[Math.min(10, categoryPaths.length)];
331332
int numberOfMissingFromCache = 0;
332333
FacetLabel cp;
333334
Integer res;

0 commit comments

Comments
 (0)