Apache Lucene open-source search software
LUCENE-6055: PayloadAttribute.clone() should deep clone its BytesRef
Shai Erera53bba16ee17d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a3d86bde86c..3e01e863239 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -226,6 +226,9 @@ Bug Fixesthe only docs in it had fields that hit non-aborting exceptionsduring indexing but also had doc values. (Mike McCandless)+* LUCENE-6055: PayloadAttribute.clone() now does a deep clone of the underlying+ bytes. (Shai Erera)+Documentation* LUCENE-5392: Add/improve analysis package documentation to reflectdiff --git a/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java b/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.javaindex 0ebd252b694..6d325a1749d 100644--- a/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java+++ b/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java@@ -88,7 +88,6 @@ public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttr@Overridepublic void fillBytesRef() {bytes.copyChars(termBuffer, 0, termLength);- bytes.get();}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.java b/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.javaindex b1dccdb8f85..b71c69bf9e1 100644--- a/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.java+++ b/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.java@@ -55,7 +55,7 @@ public class PayloadAttributeImpl extends AttributeImpl implements PayloadAttribpublic PayloadAttributeImpl clone() {PayloadAttributeImpl clone = (PayloadAttributeImpl) super.clone();if (payload != null) {- clone.payload = payload.clone();+ clone.payload = BytesRef.deepCopyOf(payload);}return clone;}@@ -86,7 +86,7 @@ public class PayloadAttributeImpl extends AttributeImpl implements PayloadAttrib@Overridepublic void copyTo(AttributeImpl target) {PayloadAttribute t = (PayloadAttribute) target;- t.setPayload((payload == null) ? null : payload.clone());+ t.setPayload((payload == null) ? null : BytesRef.deepCopyOf(payload));}diff --git a/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.java b/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.javaindex 42d795c78fd..f4c68540f80 100644--- a/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.java+++ b/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.java@@ -116,10 +116,10 @@ public abstract class AttributeImpl implements Cloneable, Attribute {* Attributes this implementation supports.*/public abstract void copyTo(AttributeImpl target);-+/**- * Shallow clone. Subclasses must override this if they- * need to clone any members deeply,+ * In most cases the clone is, and should be, deep in order to be able to+ * properly capture the state of all attributes.*/@Overridepublic AttributeImpl clone() {diff --git a/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java b/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.javaindex 747ba03d14b..bd42eb2f3ff 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java@@ -159,4 +159,21 @@ public class TestAttributeSource extends LuceneTestCase {assertTrue("The hashCode is identical, so the captured state was preserved.", hash1 != src1.hashCode());assertEquals(src2.hashCode(), src1.hashCode());}++ public void testClonePayloadAttribute() throws Exception {+ // LUCENE-6055: verify that PayloadAttribute.clone() does deep cloning.+ PayloadAttributeImpl src = new PayloadAttributeImpl(new BytesRef(new byte[] { 1, 2, 3 }));++ // test clone()+ PayloadAttributeImpl clone = src.clone();+ clone.getPayload().bytes[0] = 10; // modify one byte, srcBytes shouldn't change+ assertEquals("clone() wasn't deep", 1, src.getPayload().bytes[0]);++ // test copyTo()+ clone = new PayloadAttributeImpl();+ src.copyTo(clone);+ clone.getPayload().bytes[0] = 10; // modify one byte, srcBytes shouldn't change+ assertEquals("clone() wasn't deep", 1, src.getPayload().bytes[0]);+ }+}