Apache Lucene open-source search software
LUCENE-5880 - avoid NegativeArraySizeException in DocToDoubleVectorUtils
Tommaso Teofili1b5afe0f2695
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/utils/DocToDoubleVectorUtils.java b/lucene/classification/src/java/org/apache/lucene/classification/utils/DocToDoubleVectorUtils.javaindex 1c29255b526..c2db61e6d41 100644--- a/lucene/classification/src/java/org/apache/lucene/classification/utils/DocToDoubleVectorUtils.java+++ b/lucene/classification/src/java/org/apache/lucene/classification/utils/DocToDoubleVectorUtils.java@@ -41,7 +41,7 @@ public class DocToDoubleVectorUtils {public static Double[] toSparseLocalFreqDoubleArray(Terms docTerms, Terms fieldTerms) throws IOException {TermsEnum fieldTermsEnum = fieldTerms.iterator(null);Double[] freqVector = null;- if (docTerms != null) {+ if (docTerms != null && fieldTerms.size() > -1) {freqVector = new Double[(int) fieldTerms.size()];int i = 0;TermsEnum docTermsEnum = docTerms.iterator(null);diff --git a/lucene/classification/src/test/org/apache/lucene/classification/utils/DocToDoubleVectorUtilsTest.java b/lucene/classification/src/test/org/apache/lucene/classification/utils/DocToDoubleVectorUtilsTest.javaindex b132a1248d3..24de791e947 100644--- a/lucene/classification/src/test/org/apache/lucene/classification/utils/DocToDoubleVectorUtilsTest.java+++ b/lucene/classification/src/test/org/apache/lucene/classification/utils/DocToDoubleVectorUtilsTest.java@@ -94,12 +94,14 @@ public class DocToDoubleVectorUtilsTest extends LuceneTestCase {@Testpublic void testSparseFreqDoubleArrayConversion() throws Exception {Terms fieldTerms = MultiFields.getTerms(index, "text");- IndexSearcher indexSearcher = new IndexSearcher(index);- for (ScoreDoc scoreDoc : indexSearcher.search(new MatchAllDocsQuery(), Integer.MAX_VALUE).scoreDocs) {- Terms docTerms = index.getTermVector(scoreDoc.doc, "text");- Double[] vector = DocToDoubleVectorUtils.toSparseLocalFreqDoubleArray(docTerms, fieldTerms);- assertNotNull(vector);- assertTrue(vector.length > 0);+ if (fieldTerms != null && fieldTerms.size() != -1) {+ IndexSearcher indexSearcher = new IndexSearcher(index);+ for (ScoreDoc scoreDoc : indexSearcher.search(new MatchAllDocsQuery(), Integer.MAX_VALUE).scoreDocs) {+ Terms docTerms = index.getTermVector(scoreDoc.doc, "text");+ Double[] vector = DocToDoubleVectorUtils.toSparseLocalFreqDoubleArray(docTerms, fieldTerms);+ assertNotNull(vector);+ assertTrue(vector.length > 0);+ }}}}