Apache Lucene open-source search software
Clone the BytesRef[] values in KeywordField#newSetQuery (#12158)
Greg Miller001acaf8823b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex e52a564e365..1bbdbb31d05 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -144,7 +144,8 @@ OptimizationsBug Fixes----------------------(No changes)++* GITHUB#12158: KeywordField#newSetQuery should clone input BytesRef[] to avoid modifying provided array. (Greg Miller)Build---------------------diff --git a/lucene/core/src/java/org/apache/lucene/document/KeywordField.java b/lucene/core/src/java/org/apache/lucene/document/KeywordField.javaindex 6ff0ecaf4dd..a8abd83ea29 100644--- a/lucene/core/src/java/org/apache/lucene/document/KeywordField.java+++ b/lucene/core/src/java/org/apache/lucene/document/KeywordField.java@@ -174,7 +174,7 @@ public class KeywordField extends Field {Objects.requireNonNull(field, "field must not be null");Objects.requireNonNull(values, "values must not be null");return new IndexOrDocValuesQuery(- new TermInSetQuery(field, values), new SortedSetDocValuesSetQuery(field, values));+ new TermInSetQuery(field, values), new SortedSetDocValuesSetQuery(field, values.clone()));}/**diff --git a/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesSetQuery.java b/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesSetQuery.javaindex a742bc663bb..3f0c37cc5ac 100644--- a/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesSetQuery.java+++ b/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesSetQuery.java@@ -51,6 +51,12 @@ final class SortedSetDocValuesSetQuery extends Query implements Accountable {private final PrefixCodedTerms termData;private final int termDataHashCode; // cached hashcode of termData+ /**+ * Sole constructor. Note that {@code terms} will be sorted in-place, so callers should clone the+ * array prior to calling if they care to preserve original ordering. This decision was made since+ * this is an internal implementation (pkg-private class) and not expected to be used directly by+ * users.+ */SortedSetDocValuesSetQuery(String field, BytesRef terms[]) {this.field = Objects.requireNonNull(field);Objects.requireNonNull(terms);diff --git a/lucene/core/src/test/org/apache/lucene/document/TestKeywordField.java b/lucene/core/src/test/org/apache/lucene/document/TestKeywordField.javaindex 6593a4509c6..2ffcb6ef421 100644--- a/lucene/core/src/test/org/apache/lucene/document/TestKeywordField.java+++ b/lucene/core/src/test/org/apache/lucene/document/TestKeywordField.java@@ -26,6 +26,7 @@ import org.apache.lucene.index.SortedSetDocValues;import org.apache.lucene.index.TermsEnum;import org.apache.lucene.store.Directory;import org.apache.lucene.tests.util.LuceneTestCase;+import org.apache.lucene.tests.util.TestUtil;import org.apache.lucene.util.BytesRef;public class TestKeywordField extends LuceneTestCase {@@ -122,4 +123,17 @@ public class TestKeywordField extends LuceneTestCase {reader.close();dir.close();}++ public void testValueClone() {+ BytesRef[] values = new BytesRef[100];+ for (int i = 0; i < 100; i++) {+ String s = TestUtil.randomSimpleString(random(), 10, 20);+ values[i] = new BytesRef(s);+ }++ // Make sure we don't modify the input values array.+ BytesRef[] expected = values.clone();+ KeywordField.newSetQuery("f", values);+ assertArrayEquals(expected, values);+ }}