Buggy code:
public class DocTermOrds { private static final int TNUM_OFFSET = 2 ; public static final int DEFAULT_INDEX_INTERVAL_BITS = 7 ; private int indexIntervalBits ; private int indexIntervalMask ; private int indexInterval ; protected final int maxTermDocFreq ; protected final String field ; protected int numTermsInField ; protected long termInstances ; private long memsz ; protected int total_time ; protected int phase1_time ; protected int [ ] index ; protected byte [ ] [ ] tnums = new byte [ 256 ] [ ] ; protected long sizeOfIndexedStrings ; protected BytesRef [ ] indexedTermsArray ; protected BytesRef prefix ; protected int ordBase ; protected DocsEnum docsEnum ; public long ramUsedInBytes ( ) { } public DocTermOrds ( IndexReader reader , String field ) throws IOException { } public DocTermOrds ( IndexReader reader , String field , BytesRef termPrefix ) throws IOException { } public DocTermOrds ( IndexReader reader , String field , BytesRef termPrefix , int maxTermDocFreq ) throws IOException { } public DocTermOrds ( IndexReader reader , String field , BytesRef termPrefix , int maxTermDocFreq , int indexIntervalBits ) throws IOException { } protected DocTermOrds ( String field , int maxTermDocFreq , int indexIntervalBits ) throws IOException { } public TermsEnum getOrdTermsEnum ( IndexReader reader ) throws IOException { } protected void visitTerm ( TermsEnum te , int termNum ) throws IOException { } protected void setActualDocFreq ( int termNum , int df ) throws IOException { } protected void uninvert ( final IndexReader reader , final BytesRef termPrefix ) throws IOException { final long startTime = System . currentTimeMillis ( ) ; <START_BUG> prefix = ( termPrefix == null ) ? null : new BytesRef ( termPrefix ) ; <END_BUG> final int maxDoc = reader . maxDoc ( ) ; final int [ ] index = new int [ maxDoc ] ; final int [ ] lastTerm = new int [ maxDoc ] ; final byte [ ] [ ] bytes = new byte [ maxDoc ] [ ] ; final Terms terms = MultiFields . getTerms ( reader , field ) ; if ( terms == null ) { return ; } final TermsEnum te = terms . iterator ( null ) ; final BytesRef seekStart = ( termPrefix != null ) ? termPrefix : new BytesRef ( ) ; if ( ( te . seekCeil ( seekStart ) ) == ( SeekStatus . END ) ) { return ; } List < BytesRef > indexedTerms = null ; PagedBytes indexedTermsBytes = null ; boolean testedOrd = false ; final Bits liveDocs = MultiFields . getLiveDocs ( reader ) ; byte [ ] tempArr = new byte [ 12 ] ; int termNum = 0 ; docsEnum = null ; for ( ; ; ) { final BytesRef t = te . term ( ) ; if ( ( t == null ) || ( ( termPrefix != null ) && ( ! ( t . startsWith ( termPrefix ) ) ) ) ) { break ; } if ( ! testedOrd ) { try { ordBase = ( ( int ) ( te . ord ( ) ) ) ; } catch ( UnsupportedOperationException uoe ) { indexedTerms = new ArrayList < BytesRef > ( ) ; indexedTermsBytes = new PagedBytes ( 15 ) ; } testedOrd = true ; } visitTerm ( te , termNum ) ; if ( ( indexedTerms != null ) && ( ( termNum & ( indexIntervalMask ) ) == 0 ) ) { sizeOfIndexedStrings += t . length ; BytesRef indexedTerm = new BytesRef ( ) ; indexedTermsBytes . copy ( t , indexedTerm ) ; indexedTerms . add ( indexedTerm ) ; } final int df = te . docFreq ( ) ; if ( df <= ( maxTermDocFreq ) ) { docsEnum = te . docs ( liveDocs , docsEnum ) ; final DocsEnum . BulkReadResult bulkResult = docsEnum . getBulkResult ( ) ; int actualDF = 0 ; for ( ; ; ) { int chunk = docsEnum . read ( ) ; if ( chunk <= 0 ) { break ; } actualDF += chunk ; for ( int i = 0 ; i < chunk ; i ++ ) { ( termInstances ) ++ ; int doc = bulkResult . docs . ints [ i ] ; int delta = ( termNum - ( lastTerm [ doc ] ) ) + ( DocTermOrds . TNUM_OFFSET ) ; lastTerm [ doc ] = termNum ; int val = index [ doc ] ; if ( ( val & 255 ) == 1 ) { int pos = val > > > 8 ; int ilen = DocTermOrds . vIntSize ( delta ) ; byte [ ] arr = bytes [ doc ] ; int newend = pos + ilen ; if ( newend > ( arr . length ) ) { int newLen = ( newend + 3 ) & - 4 ; byte [ ] newarr = new byte [ newLen ] ; System . arraycopy ( arr , 0 , newarr , 0 , pos ) ; arr = newarr ; bytes [ doc ] = newarr ; } pos = DocTermOrds . writeInt ( delta , arr , pos ) ; index [ doc ] = ( pos << 8 ) | 1 ; } else { int ipos ; if ( val == 0 ) { ipos = 0 ; } else if ( ( val & 65408 ) == 0 ) { ipos = 1 ; } else if ( ( val & 16744448 ) == 0 ) { ipos = 2 ; } else if ( ( val & - 8388608 ) == 0 ) { ipos = 3 ; } else { ipos = 4 ; } int endPos = DocTermOrds . writeInt ( delta , tempArr , ipos ) ; if ( endPos <= 4 ) { for ( int j = ipos ; j < endPos ;

Fix:
prefix = ( termPrefix == null ) ? null : BytesRef . deepCopyOf ( termPrefix ) ;