Buggy code:
public class TestTermsEnum extends LuceneTestCase { public void test ( ) throws Exception { } private Directory d ; private IndexReader r ; private final String FIELD = "field" ; private IndexReader makeIndex ( String ... terms ) throws Exception { } private void close ( ) throws Exception { } private int docFreq ( IndexReader r , String term ) throws Exception { } public void testEasy ( ) throws Exception { } public void testFloorBlocks ( ) throws Exception { } public void testZeroTerms ( ) throws Exception { } private String getRandomString ( ) { } public void testRandomTerms ( ) throws Exception { } private BytesRef getNonExistTerm ( BytesRef [ ] terms ) { } private void testRandomSeeks ( IndexReader r , String ... validTermStrings ) throws IOException { final BytesRef [ ] validTerms = new BytesRef [ validTermStrings . length ] ; for ( int termIDX = 0 ; termIDX < ( validTermStrings . length ) ; termIDX ++ ) { validTerms [ termIDX ] = new BytesRef ( validTermStrings [ termIDX ] ) ; } Arrays . sort ( validTerms , BytesRef . getUTF8SortedAsUTF16Comparator ( ) ) ; if ( VERBOSE ) { System . out . println ( ( ( "TEST:<seq2seq4repair_space>" + ( validTerms . length ) ) + "<seq2seq4repair_space>terms:" ) ) ; for ( int idx = 0 ; idx < ( validTerms . length ) ; idx ++ ) { System . out . println ( ( ( ( "<seq2seq4repair_space>" + idx ) + ":<seq2seq4repair_space>" ) + ( validTerms [ idx ] ) ) ) ; } } final int END_LOC = ( - ( validTerms . length ) ) - 1 ; for ( int iter = 0 ; iter < ( 100 * ( RANDOM_MULTIPLIER ) ) ; iter ++ ) { final BytesRef t ; int loc ; if ( ( random . nextInt ( 6 ) ) == 4 ) { t = getNonExistTerm ( validTerms ) ; if ( VERBOSE ) { System . out . println ( ( "\nTEST:<seq2seq4repair_space>invalid<seq2seq4repair_space>term=" + ( t . utf8ToString ( ) ) ) ) ; } loc = Arrays . binarySearch ( validTerms , t , BytesRef . getUTF8SortedAsUTF16Comparator ( ) ) ; } else { loc = random . nextInt ( validTerms . length ) ; <START_BUG> t = new BytesRef ( validTerms [ loc ] ) ; <END_BUG> if ( VERBOSE ) { System . out . println ( ( "\nTEST:<seq2seq4repair_space>valid<seq2seq4repair_space>term=" + ( t . utf8ToString ( ) ) ) ) ; } } final Term targetTerm = new Term ( FIELD , t . utf8ToString ( ) ) ; if ( VERBOSE ) { System . out . println ( ( "<seq2seq4repair_space>seek<seq2seq4repair_space>term=" + targetTerm ) ) ; } final TermEnum te = r . terms ( targetTerm ) ; Term actualTerm = te . term ( ) ; if ( VERBOSE ) { System . out . println ( ( "<seq2seq4repair_space>got<seq2seq4repair_space>" + actualTerm ) ) ; } if ( loc >= 0 ) { } else if ( loc == END_LOC ) { assertTrue ( ( ( actualTerm == null ) || ( ! ( FIELD . equals ( actualTerm . field ( ) ) ) ) ) ) ; } else { assert loc >= ( - ( validTerms . length ) ) ; assertTrue ( ( ( actualTerm != null ) && ( FIELD . equals ( actualTerm . field ( ) ) ) ) ) ; } if ( loc >= 0 ) { assertEquals ( targetTerm , actualTerm ) ; } else if ( loc == END_LOC ) { continue ; } else { loc = ( - loc ) - 1 ; assertEquals ( new Term ( FIELD , validTerms [ loc ] . utf8ToString ( ) ) , actualTerm ) ; } final int numNext = random . nextInt ( validTerms . length ) ; if ( VERBOSE ) { System . out . println ( ( "\nTEST:<seq2seq4repair_space>numNext=" + numNext ) ) ; } for ( int nextCount = 0 ; nextCount < numNext ; nextCount ++ ) { if ( VERBOSE ) { System . out . println ( ( ( ( "\nTEST:<seq2seq4repair_space>next<seq2seq4repair_space>loc=" + loc ) + "<seq2seq4repair_space>of<seq2seq4repair_space>" ) + ( validTerms . length ) ) ) ; } boolean result = te . next ( ) ; actualTerm = te . term ( ) ; loc ++ ; if ( loc == ( validTerms . length ) ) { if ( VERBOSE ) { System . out . println ( "<seq2seq4repair_space>actual=null" ) ; } assertFalse ( result ) ; assertTrue ( ( ( actualTerm == null ) || ( ! ( FIELD . equals ( actualTerm . field ( ) ) ) ) ) ) ; break ; } else { if ( VERBOSE ) { System . out . println ( ( "<seq2seq4repair_space>actual=" + ( new BytesRef ( actualTerm . text ( ) ) ) ) ) ; } assertTrue ( result ) ; assertTrue ( ( ( actualTerm != null ) && ( FIELD . equals ( actualTerm . field ( ) ) ) ) ) ; assertEquals ( validTerms [ loc ] , new BytesRef ( actualTerm . text ( ) ) ) ; } } } } }

Fix:
t = BytesRef . deepCopyOf ( validTerms [ loc ] ) ;