lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jpou...@apache.org
Subject [1/2] lucene-solr:branch_7x: LUCENE-8165: Ban copyOf and copyOfRange.
Date Thu, 07 Jun 2018 09:15:04 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x e691bf734 -> 42b69f39e


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.java b/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.java
index 7a2712f..cb743f1 100644
--- a/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.java
+++ b/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.java
@@ -16,9 +16,6 @@
  */
 package org.apache.lucene.util;
 
-
-import java.util.Arrays;
-
 /**
  * Estimates how {@link RamUsageEstimator} estimates physical memory consumption
  * of Java objects. 
@@ -88,7 +85,7 @@ public class StressRamUsageEstimator extends LuceneTestCase {
 
         // Make another batch of objects.
         Object[] seg =  new Object[10000];
-        all = Arrays.copyOf(all, all.length + 1);
+        all = ArrayUtil.growExact(all, all.length + 1);
         all[all.length - 1] = seg;
         for (int i = 0; i < seg.length; i++) {
           seg[i] = new byte[random().nextInt(7)];

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java b/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java
index 0cda337..285df74 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java
@@ -21,6 +21,9 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.Random;
 
+import static org.apache.lucene.util.ArrayUtil.copyOfSubArray;
+import static org.apache.lucene.util.ArrayUtil.growExact;
+
 public class TestArrayUtil extends LuceneTestCase {
 
   // Ensure ArrayUtil.getNextSize gives linear amortized cost of realloc/copy
@@ -294,4 +297,88 @@ public class TestArrayUtil extends LuceneTestCase {
       }
     }
   }
+
+  public void testGrowExact() {
+    assertArrayEquals(new short[]{1, 2, 3, 0}, growExact(new short[]{1, 2, 3}, 4));
+    assertArrayEquals(new short[]{1, 2, 3, 0, 0}, growExact(new short[]{1, 2, 3}, 5));
+    expectThrows(IndexOutOfBoundsException.class, () -> growExact(new short[]{1, 2, 3},
random().nextInt(3)));
+
+    assertArrayEquals(new int[]{1, 2, 3, 0}, growExact(new int[]{1, 2, 3}, 4));
+    assertArrayEquals(new int[]{1, 2, 3, 0, 0}, growExact(new int[]{1, 2, 3}, 5));
+    expectThrows(IndexOutOfBoundsException.class, () -> growExact(new int[]{1, 2, 3},
random().nextInt(3)));
+
+    assertArrayEquals(new long[]{1, 2, 3, 0}, growExact(new long[]{1, 2, 3}, 4));
+    assertArrayEquals(new long[]{1, 2, 3, 0, 0}, growExact(new long[]{1, 2, 3}, 5));
+    expectThrows(IndexOutOfBoundsException.class, () -> growExact(new long[]{1, 2, 3},
random().nextInt(3)));
+
+    assertArrayEquals(new float[]{0.1f, 0.2f, 0.3f, 0}, growExact(new float[]{0.1f, 0.2f,
0.3f}, 4), 0.001f);
+    assertArrayEquals(new float[]{0.1f, 0.2f, 0.3f, 0, 0}, growExact(new float[]{0.1f, 0.2f,
0.3f}, 5), 0.001f);
+    expectThrows(IndexOutOfBoundsException.class, () -> growExact(new float[]{1, 2, 3},
random().nextInt(3)));
+
+    assertArrayEquals(new double[]{0.1, 0.2, 0.3, 0.0}, growExact(new double[]{0.1, 0.2,
0.3}, 4), 0.001);
+    assertArrayEquals(new double[]{0.1, 0.2, 0.3, 0.0, 0.0}, growExact(new double[]{0.1,
0.2, 0.3}, 5), 0.001);
+    expectThrows(IndexOutOfBoundsException.class, () -> growExact(new double[]{0.1, 0.2,
0.3}, random().nextInt(3)));
+
+    assertArrayEquals(new byte[]{1, 2, 3, 0}, growExact(new byte[]{1, 2, 3}, 4));
+    assertArrayEquals(new byte[]{1, 2, 3, 0, 0}, growExact(new byte[]{1, 2, 3}, 5));
+    expectThrows(IndexOutOfBoundsException.class, () -> growExact(new byte[]{1, 2, 3},
random().nextInt(3)));
+
+    assertArrayEquals(new char[]{'a', 'b', 'c', '\0'}, growExact(new char[]{'a', 'b', 'c'},
4));
+    assertArrayEquals(new char[]{'a', 'b', 'c', '\0', '\0'}, growExact(new char[]{'a', 'b',
'c'}, 5));
+    expectThrows(IndexOutOfBoundsException.class, () -> growExact(new byte[]{'a', 'b',
'c'}, random().nextInt(3)));
+
+    assertArrayEquals(new String[]{"a1", "b2", "c3", null}, growExact(new String[]{"a1",
"b2", "c3"}, 4));
+    assertArrayEquals(new String[]{"a1", "b2", "c3", null, null}, growExact(new String[]{"a1",
"b2", "c3"}, 5));
+    expectThrows(IndexOutOfBoundsException.class, () -> growExact(new String[]{"a", "b",
"c"}, random().nextInt(3)));
+  }
+
+  public void testCopyOfSubArray() {
+    short[] shortArray = {1, 2, 3};
+    assertArrayEquals(new short[]{1}, copyOfSubArray(shortArray, 0, 1));
+    assertArrayEquals(new short[]{1, 2, 3}, copyOfSubArray(shortArray, 0, 3));
+    assertEquals(0, copyOfSubArray(shortArray, 0, 0).length);
+    expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(shortArray, 0,
4 + random().nextInt(10)));
+
+    int[] intArray = {1, 2, 3};
+    assertArrayEquals(new int[]{1, 2}, copyOfSubArray(intArray, 0, 2));
+    assertArrayEquals(new int[]{1, 2, 3}, copyOfSubArray(intArray, 0, 3));
+    assertEquals(0, copyOfSubArray(intArray, 1, 1).length);
+    expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(intArray, 1, 4
+ random().nextInt(10)));
+
+    long[] longArray = {1, 2, 3};
+    assertArrayEquals(new long[]{2}, copyOfSubArray(longArray, 1, 2));
+    assertArrayEquals(new long[]{1, 2, 3}, copyOfSubArray(longArray, 0, 3));
+    assertEquals(0, copyOfSubArray(longArray, 2, 2).length);
+    expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(longArray, 2, 4
+ random().nextInt(10)));
+
+    float[] floatArray = {0.1f, 0.2f, 0.3f};
+    assertArrayEquals(new float[]{0.2f, 0.3f}, copyOfSubArray(floatArray, 1, 3), 0.001f);
+    assertArrayEquals(new float[]{0.1f, 0.2f, 0.3f}, copyOfSubArray(floatArray, 0, 3), 0.001f);
+    assertEquals(0, copyOfSubArray(floatArray, 0, 0).length);
+    expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(floatArray, 3,
4 + random().nextInt(10)));
+
+    double[] doubleArray = {0.1, 0.2, 0.3};
+    assertArrayEquals(new double[]{0.3}, copyOfSubArray(doubleArray, 2, 3), 0.001);
+    assertArrayEquals(new double[]{0.1, 0.2, 0.3}, copyOfSubArray(doubleArray, 0, 3), 0.001);
+    assertEquals(0, copyOfSubArray(doubleArray, 1, 1).length);
+    expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(doubleArray, 0,
4 + random().nextInt(10)));
+
+    byte[] byteArray = {1, 2, 3};
+    assertArrayEquals(new byte[]{1}, copyOfSubArray(byteArray, 0, 1));
+    assertArrayEquals(new byte[]{1, 2, 3}, copyOfSubArray(byteArray, 0, 3));
+    assertEquals(0, copyOfSubArray(byteArray, 1, 1).length);
+    expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(byteArray, 1, 4
+ random().nextInt(10)));
+
+    char[] charArray = {'a', 'b', 'c'};
+    assertArrayEquals(new char[]{'a', 'b'}, copyOfSubArray(charArray, 0, 2));
+    assertArrayEquals(new char[]{'a', 'b', 'c'}, copyOfSubArray(charArray, 0, 3));
+    assertEquals(0, copyOfSubArray(charArray, 1, 1).length);
+    expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(charArray, 3, 4));
+
+    String[] objectArray = {"a1", "b2", "c3"};
+    assertArrayEquals(new String[]{"a1"}, copyOfSubArray(objectArray, 0, 1));
+    assertArrayEquals(new String[]{"a1", "b2", "c3"}, copyOfSubArray(objectArray, 0, 3));
+    assertEquals(0, copyOfSubArray(objectArray, 1, 1).length);
+    expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(objectArray, 2,
5));
+  }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.java b/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.java
index 3a5bb53..2a869ad 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.java
@@ -48,4 +48,12 @@ public class TestBytesRef extends LuceneTestCase {
     // only for 4.x
     assertEquals("\uFFFF", new BytesRef("\uFFFF").utf8ToString());
   }
+
+  public void testInvalidDeepCopy() {
+    BytesRef from = new BytesRef(new byte[] { 1, 2 });
+    from.offset += 1; // now invalid
+    expectThrows(IndexOutOfBoundsException.class, () -> {
+      BytesRef.deepCopyOf(from);
+    });
+  }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java b/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java
index 0a4c884..079b3b7 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java
@@ -125,4 +125,12 @@ public class TestCharsRef extends LuceneTestCase {
       c.subSequence(2, 1);
     });
   }
+  
+  public void testInvalidDeepCopy() {
+    CharsRef from = new CharsRef(new char[] { 'a', 'b' }, 0, 2);
+    from.offset += 1; // now invalid
+    expectThrows(IndexOutOfBoundsException.class, () -> {
+      CharsRef.deepCopyOf(from);
+    });
+  }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.java b/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.java
index b997659..654e77d 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.java
@@ -37,4 +37,12 @@ public class TestIntsRef extends LuceneTestCase {
     
     assertFalse(i.equals(i2));
   }
+  
+  public void testInvalidDeepCopy() {
+    IntsRef from = new IntsRef(new int[] { 1, 2 }, 0, 2);
+    from.offset += 1; // now invalid
+    expectThrows(IndexOutOfBoundsException.class, () -> {
+      IntsRef.deepCopyOf(from);
+    });
+  }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.java b/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.java
index ba8bd02..b7696c2 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.java
@@ -37,7 +37,7 @@ public class TestLSBRadixSorter extends LuceneTestCase {
   }
 
   public void test(LSBRadixSorter sorter, int[] arr, int len) {
-    final int[] expected = Arrays.copyOf(arr, len);
+    final int[] expected = ArrayUtil.copyOfSubArray(arr, 0, len);
     Arrays.sort(expected);
 
     int numBits = 0;
@@ -50,7 +50,7 @@ public class TestLSBRadixSorter extends LuceneTestCase {
     }
 
     sorter.sort(numBits, arr, len);
-    final int[] actual = Arrays.copyOf(arr, len);
+    final int[] actual = ArrayUtil.copyOfSubArray(arr, 0, len);
     assertArrayEquals(expected, actual);
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/TestLongsRef.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestLongsRef.java b/lucene/core/src/test/org/apache/lucene/util/TestLongsRef.java
new file mode 100644
index 0000000..ec4575f
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/util/TestLongsRef.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.util;
+
+public class TestLongsRef extends LuceneTestCase {
+  public void testEmpty() {
+    LongsRef i = new LongsRef();
+    assertEquals(LongsRef.EMPTY_LONGS, i.longs);
+    assertEquals(0, i.offset);
+    assertEquals(0, i.length);
+  }
+  
+  public void testFromLongs() {
+    long longs[] = new long[] { 1, 2, 3, 4 };
+    LongsRef i = new LongsRef(longs, 0, 4);
+    assertEquals(longs, i.longs);
+    assertEquals(0, i.offset);
+    assertEquals(4, i.length);
+    
+    LongsRef i2 = new LongsRef(longs, 1, 3);
+    assertEquals(new LongsRef(new long[] { 2, 3, 4 }, 0, 3), i2);
+    
+    assertFalse(i.equals(i2));
+  }
+  
+  public void testInvalidDeepCopy() {
+    LongsRef from = new LongsRef(new long[] { 1, 2 }, 0, 2);
+    from.offset += 1; // now invalid
+    expectThrows(IndexOutOfBoundsException.class, () -> {
+      LongsRef.deepCopyOf(from);
+    });
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.java b/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.java
index 52eb494..efd1f03 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.java
@@ -23,7 +23,7 @@ import java.util.Set;
 public class TestMSBRadixSorter extends LuceneTestCase {
 
   private void test(BytesRef[] refs, int len) {
-    BytesRef[] expected = Arrays.copyOf(refs, len);
+    BytesRef[] expected = ArrayUtil.copyOfSubArray(refs, 0, len);
     Arrays.sort(expected);
 
     int maxLength = 0;
@@ -63,7 +63,7 @@ public class TestMSBRadixSorter extends LuceneTestCase {
         refs[j] = tmp;
       }
     }.sort(0, len);
-    BytesRef[] actual = Arrays.copyOf(refs, len);
+    BytesRef[] actual = ArrayUtil.copyOfSubArray(refs, 0, len);
     assertArrayEquals(expected, actual);
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.java b/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.java
index c83ff67..c4ee68b 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.java
@@ -21,7 +21,7 @@ import java.util.Arrays;
 public class TestStringMSBRadixSorter extends LuceneTestCase {
 
   private void test(BytesRef[] refs, int len) {
-    BytesRef[] expected = Arrays.copyOf(refs, len);
+    BytesRef[] expected = ArrayUtil.copyOfSubArray(refs, 0, len);
     Arrays.sort(expected);
 
     new StringMSBRadixSorter() {
@@ -38,7 +38,7 @@ public class TestStringMSBRadixSorter extends LuceneTestCase {
         refs[j] = tmp;
       }
     }.sort(0, len);
-    BytesRef[] actual = Arrays.copyOf(refs, len);
+    BytesRef[] actual = ArrayUtil.copyOfSubArray(refs, 0, len);
     assertArrayEquals(expected, actual);
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
index a675e0b..69c1b3f 100644
--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
@@ -34,6 +34,7 @@ import org.apache.lucene.store.IOContext;
 import org.apache.lucene.store.IndexInput;
 import org.apache.lucene.store.IndexOutput;
 import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.util.ArrayUtil;
 import org.apache.lucene.util.LongValues;
 import org.apache.lucene.util.LongsRef;
 import org.apache.lucene.util.LuceneTestCase;
@@ -903,8 +904,8 @@ public class TestPackedInts extends LuceneTestCase {
         // 3. re-encode
         final long[] blocks2 = new long[blocksOffset2 + blocksLen];
         encoder.encode(values, valuesOffset, blocks2, blocksOffset2, longIterations);
-        assertArrayEquals(msg, Arrays.copyOfRange(blocks, blocksOffset, blocks.length),
-            Arrays.copyOfRange(blocks2, blocksOffset2, blocks2.length));
+        assertArrayEquals(msg, ArrayUtil.copyOfSubArray(blocks, blocksOffset, blocks.length),
+            ArrayUtil.copyOfSubArray(blocks2, blocksOffset2, blocks2.length));
         // test encoding from int[]
         if (bpv <= 32) {
           final long[] blocks3 = new long[blocks2.length];

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.java
----------------------------------------------------------------------
diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.java
b/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.java
index cb24f6f..fe5317e 100644
--- a/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.java
+++ b/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.java
@@ -40,6 +40,7 @@ import org.apache.lucene.search.SortField;
 import org.apache.lucene.search.TermQuery;
 import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.ArrayUtil;
 import org.apache.lucene.util.English;
 import org.apache.lucene.util.LuceneTestCase;
 import org.apache.lucene.util.TestUtil;
@@ -109,7 +110,7 @@ public class TestExpressionSorts extends LuceneTestCase {
       };
       Collections.shuffle(Arrays.asList(fields), random());
       int numSorts = TestUtil.nextInt(random(), 1, fields.length);
-      assertQuery(query, new Sort(Arrays.copyOfRange(fields, 0, numSorts)));
+      assertQuery(query, new Sort(ArrayUtil.copyOfSubArray(fields, 0, numSorts)));
     }
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.java
b/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.java
index cd1dca1..5894012 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.java
@@ -18,7 +18,6 @@
 package org.apache.lucene.search.intervals;
 
 import java.io.IOException;
-import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Objects;
 import java.util.Set;
@@ -34,6 +33,7 @@ import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.TermStatistics;
 import org.apache.lucene.search.Weight;
 import org.apache.lucene.search.similarities.Similarity;
+import org.apache.lucene.util.ArrayUtil;
 
 /**
  * A query that retrieves documents containing intervals returned from an
@@ -87,7 +87,7 @@ public final class IntervalQuery extends Query {
       return null;
     }
     CollectionStatistics collectionStats = searcher.collectionStatistics(field);
-    return searcher.getSimilarity(needsScores).computeWeight(boost, collectionStats, Arrays.copyOf(termStats,
termUpTo));
+    return searcher.getSimilarity(needsScores).computeWeight(boost, collectionStats, ArrayUtil.copyOfSubArray(termStats,
0, termUpTo));
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.java
index 0bcb3f8..9f70808 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.java
@@ -89,7 +89,7 @@ public class TestHalfFloatPoint extends LuceneTestCase {
         values[o++] = v;
       }
     }
-    values = Arrays.copyOf(values, o);
+    values = ArrayUtil.copyOfSubArray(values, 0, o);
 
     int iters = atLeast(1000000);
     for (int iter = 0; iter < iters; ++iter) {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.java
----------------------------------------------------------------------
diff --git a/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.java
b/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.java
index 3242e7e..9bc947f 100644
--- a/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.java
+++ b/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.java
@@ -19,7 +19,6 @@ package org.apache.lucene.spatial.prefix.tree;
 import java.io.PrintStream;
 import java.text.NumberFormat;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Locale;
@@ -245,7 +244,8 @@ public class QuadPrefixTree extends LegacyPrefixTree {
 
     protected BytesRef concat(BytesRef source, byte b) {
       //+2 for new char + potential leaf
-      final byte[] buffer = Arrays.copyOfRange(source.bytes, source.offset, source.offset
+ source.length + 2);
+      final byte[] buffer = new byte[source.length + 2];
+      System.arraycopy(source.bytes, source.offset, buffer, 0, source.length);
       BytesRef target = new BytesRef(buffer);
       target.length = source.length;
       target.bytes[target.length++] = b;

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.java
----------------------------------------------------------------------
diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.java
b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.java
index 3e3b2e2..989252e 100644
--- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.java
+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.java
@@ -18,7 +18,6 @@ package org.apache.lucene.spatial;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 
 import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
@@ -31,6 +30,7 @@ import org.apache.lucene.spatial.prefix.tree.QuadPrefixTree;
 import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;
 import org.apache.lucene.spatial.serialized.SerializedDVStrategy;
 import org.apache.lucene.spatial.vector.PointVectorStrategy;
+import org.apache.lucene.util.ArrayUtil;
 import org.junit.Test;
 import org.locationtech.spatial4j.context.SpatialContext;
 import org.locationtech.spatial4j.shape.Point;
@@ -107,7 +107,7 @@ public class DistanceStrategyTest extends StrategyTestCase {
 
   void checkDistValueSource(Point pt, float... distances) throws IOException {
     float multiplier = random().nextFloat() * 100f;
-    float[] dists2 = Arrays.copyOf(distances, distances.length);
+    float[] dists2 = ArrayUtil.copyOfSubArray(distances, 0, distances.length);
     for (int i = 0; i < dists2.length; i++) {
       dists2[i] *= multiplier;
     }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
index 2c5dcd8..c25b44d 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
@@ -17,7 +17,6 @@
 package org.apache.lucene.search.suggest.document;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Comparator;
 import java.util.HashSet;
 import java.util.List;
@@ -521,7 +520,7 @@ public class TestContextQuery extends LuceneTestCase {
           query.addContext(contexts.get(i), i + 1);
         }
         TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 4, false);
-        assertSuggestions(suggest, Arrays.copyOfRange(expectedResults, 0, 4));
+        assertSuggestions(suggest, ArrayUtil.copyOfSubArray(expectedResults, 0, 4));
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.java
----------------------------------------------------------------------
diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.java
b/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.java
index 60e2cca..82d8adb 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.java
@@ -559,7 +559,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
       for (int j = 0; j < data[docId].length; ++j) {
         final byte[] arr = data[docId][j];
         final BytesRef arr2Ref = doc.getBinaryValue("bytes" + j);
-        final byte[] arr2 = Arrays.copyOfRange(arr2Ref.bytes, arr2Ref.offset, arr2Ref.offset
+ arr2Ref.length);
+        final byte[] arr2 = BytesRef.deepCopyOf(arr2Ref).bytes;
         assertArrayEquals(arr, arr2);
       }
     }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java
----------------------------------------------------------------------
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java
b/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java
index 8ef4feb..e3f26e4 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java
@@ -17,7 +17,6 @@
 package org.apache.lucene.util.automaton;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -253,8 +252,7 @@ public class AutomatonTestUtil {
         codePoints[codepointCount++] = getRandomCodePoint(r, t.min, t.max);
         s = t.dest;
       }
-
-      return Arrays.copyOf(codePoints, codepointCount);
+      return ArrayUtil.copyOfSubArray(codePoints, 0, codepointCount);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/42b69f39/lucene/tools/forbiddenApis/lucene.txt
----------------------------------------------------------------------
diff --git a/lucene/tools/forbiddenApis/lucene.txt b/lucene/tools/forbiddenApis/lucene.txt
index e02bd40..2a1d883 100644
--- a/lucene/tools/forbiddenApis/lucene.txt
+++ b/lucene/tools/forbiddenApis/lucene.txt
@@ -27,3 +27,27 @@ java.util.zip.ZipFile
 @defaultMessage Use home-grown methods instead
 java.lang.Math#toRadians(double)
 java.lang.Math#toDegrees(double)
+
+@defaultMessage Prefer using ArrayUtil as Arrays#copyOfRange fills zeros for bad bounds
+java.util.Arrays#copyOfRange(byte[],int,int)
+java.util.Arrays#copyOfRange(char[],int,int)
+java.util.Arrays#copyOfRange(short[],int,int)
+java.util.Arrays#copyOfRange(int[],int,int)
+java.util.Arrays#copyOfRange(long[],int,int)
+java.util.Arrays#copyOfRange(float[],int,int)
+java.util.Arrays#copyOfRange(double[],int,int)
+java.util.Arrays#copyOfRange(boolean[],int,int)
+java.util.Arrays#copyOfRange(java.lang.Object[],int,int)
+java.util.Arrays#copyOfRange(java.lang.Object[],int,int,java.lang.Class)
+
+@defaultMessage Prefer using ArrayUtil as Arrays#copyOf fills zeros for bad bounds
+java.util.Arrays#copyOf(byte[],int)
+java.util.Arrays#copyOf(char[],int)
+java.util.Arrays#copyOf(short[],int)
+java.util.Arrays#copyOf(int[],int)
+java.util.Arrays#copyOf(long[],int)
+java.util.Arrays#copyOf(float[],int)
+java.util.Arrays#copyOf(double[],int)
+java.util.Arrays#copyOf(boolean[],int)
+java.util.Arrays#copyOf(java.lang.Object[],int)
+java.util.Arrays#copyOf(java.lang.Object[],int,java.lang.Class)


Mime
View raw message