Buggy code:
public class BlockTreeTermsWriter extends FieldsConsumer { public static final int DEFAULT_MIN_BLOCK_SIZE = 25 ; public static final int DEFAULT_MAX_BLOCK_SIZE = 48 ; public static final boolean SAVE_DOT_FILES = false ; static final int OUTPUT_FLAGS_NUM_BITS = 2 ; static final int OUTPUT_FLAGS_MASK = 3 ; static final int OUTPUT_FLAG_IS_FLOOR = 1 ; static final int OUTPUT_FLAG_HAS_TERMS = 2 ; static final String CODEC_NAME = "BLOCK_TREE_TERMS_DICT" ; public static final int VERSION_START = 0 ; public static final int VERSION_CURRENT = BlockTreeTermsWriter . VERSION_START ; static final String TERMS_EXTENSION = "tim" ; static final String TERMS_INDEX_EXTENSION = "tip" ; protected final IndexOutput out ; private final IndexOutput indexOut ; final int minItemsInBlock ; final int maxItemsInBlock ; final PostingsWriterBase postingsWriter ; final FieldInfos fieldInfos ; FieldInfo currentField ; private final List < BlockTreeTermsWriter . TermsWriter > fields = new ArrayList < BlockTreeTermsWriter . TermsWriter > ( ) ; public BlockTreeTermsWriter ( SegmentWriteState state , PostingsWriterBase postingsWriter , int minItemsInBlock , int maxItemsInBlock ) throws IOException { } protected void writeHeader ( IndexOutput out ) throws IOException { } protected void writeIndexHeader ( IndexOutput out ) throws IOException { } protected void writeTrailer ( long dirStart ) throws IOException { } protected void writeIndexTrailer ( long dirStart ) throws IOException { } @ Override public TermsConsumer addField ( FieldInfo field ) throws IOException { } static long encodeOutput ( long fp , boolean hasTerms , boolean isFloor ) { } private static class PendingEntry { public final boolean isTerm ; protected PendingEntry ( boolean isTerm ) { } } private static final class PendingTerm extends BlockTreeTermsWriter . PendingEntry { public final BytesRef term ; public final TermStats stats ; public PendingTerm ( BytesRef term , TermStats stats ) { } @ Override public String toString ( ) { } } private static final class PendingBlock extends BlockTreeTermsWriter . PendingEntry { public final BytesRef prefix ; public final long fp ; public FST < BytesRef > index ; public List < FST < BytesRef > > subIndices ; public final boolean hasTerms ; public final boolean isFloor ; public final int floorLeadByte ; public PendingBlock ( BytesRef prefix , long fp , boolean hasTerms , boolean isFloor , int floorLeadByte , List < FST < BytesRef > > subIndices ) { } @ Override public String toString ( ) { } public void compileIndex ( List < BlockTreeTermsWriter . PendingBlock > floorBlocks , RAMOutputStream scratchBytes ) throws IOException { } private void append ( Builder < BytesRef > builder , FST < BytesRef > subIndex ) throws IOException { } } final RAMOutputStream scratchBytes = new RAMOutputStream ( ) ; class TermsWriter extends TermsConsumer { private final FieldInfo fieldInfo ; private long numTerms ; long sumTotalTermFreq ; long sumDocFreq ; int docCount ; long indexStartFP ; private final NoOutputs noOutputs ; private final Builder < Object > blockBuilder ; private final List < BlockTreeTermsWriter . PendingEntry > pending = new ArrayList < BlockTreeTermsWriter . PendingEntry > ( ) ; private int lastBlockIndex = - 1 ; private int [ ] subBytes = new int [ 10 ] ; private int [ ] subTermCounts = new int [ 10 ] ; private int [ ] subTermCountSums = new int [ 10 ] ; private int [ ] subSubCounts = new int [ 10 ] ; private class FindBlocks extends Builder . FreezeTail < Object > { @ Override public void freeze ( final Builder < Object > [ ] frontier , int prefixLenPlus1 , final IntsRef lastInput ) throws IOException { } } void writeBlocks ( IntsRef prevTerm , int prefixLength , int count ) throws IOException { } private String toString ( BytesRef b ) { } private BlockTreeTermsWriter . PendingBlock writeBlock ( IntsRef prevTerm , int prefixLength , int indexPrefixLength , int startBackwards , int length , int futureTermCount , boolean isFloor , int floorLeadByte , boolean isLastInFloor ) throws IOException { } TermsWriter ( FieldInfo fieldInfo ) { } @ Override public Comparator < BytesRef > getComparator ( ) { } @ Override public PostingsConsumer startTerm ( BytesRef text ) throws IOException { } @ Override public void finishTerm ( BytesRef text , TermStats stats ) throws IOException { assert ( stats . docFreq ) > 0 ; blockBuilder . add ( text , noOutputs . getNoOutput ( ) ) ; <START_BUG> pending . add ( new BlockTreeTermsWriter . PendingTerm ( new BytesRef ( text ) , stats ) ) ; <END_BUG> postingsWriter . finishTerm ( stats ) ; ( numTerms ) ++ ; } @ Override public void finish ( long sumTotalTermFreq , long sumDocFreq , int docCount ) throws IOException { } private final RAMOutputStream bytesWriter = new RAMOutputStream ( ) ; private final RAMOutputStream bytesWriter2 = new RAMOutputStream ( ) ; } @ Override public void close ( ) throws IOException { } }

Fix:
pending . add ( new BlockTreeTermsWriter . PendingTerm ( BytesRef . deepCopyOf ( text ) , stats ) ) ;