Buggy code:
@ AfterClass public static void afterClass ( ) throws Exception { } private FieldInfos currentFieldInfos ; private FieldsProducer buildIndex ( Directory dir , IndexOptions maxAllowed , boolean allowPayloads ) throws IOException { } private static class ThreadState { public DocsEnum reuseDocsEnum ; public DocsAndPositionsEnum reuseDocsAndPositionsEnum ; } private void verifyEnum ( TestPostingsFormat . ThreadState threadState , String field , BytesRef term , TermsEnum termsEnum , IndexOptions maxIndexOptions , EnumSet < TestPostingsFormat . Option > options ) throws IOException { if ( VERBOSE ) { System . out . println ( ( ( ( "<seq2seq4repair_space>verifyEnum:<seq2seq4repair_space>options=" + options ) + "<seq2seq4repair_space>maxIndexOptions=" ) + maxIndexOptions ) ) ; } Bits liveDocs ; Map < String , Map < BytesRef , List < TestPostingsFormat . Posting > > > fieldsToUse ; if ( ( options . contains ( TestPostingsFormat . Option . LIVE_DOCS ) ) && ( random ( ) . nextBoolean ( ) ) ) { liveDocs = TestPostingsFormat . globalLiveDocs ; fieldsToUse = TestPostingsFormat . fieldsLive ; if ( VERBOSE ) { System . out . println ( "<seq2seq4repair_space>use<seq2seq4repair_space>liveDocs" ) ; } } else { liveDocs = null ; fieldsToUse = TestPostingsFormat . fields ; if ( VERBOSE ) { System . out . println ( "<seq2seq4repair_space>no<seq2seq4repair_space>liveDocs" ) ; } } FieldInfo fieldInfo = currentFieldInfos . fieldInfo ( field ) ; assertEquals ( TestPostingsFormat . fields . get ( field ) . get ( term ) . size ( ) , termsEnum . docFreq ( ) ) ; List < TestPostingsFormat . Posting > expected = fieldsToUse . get ( field ) . get ( term ) ; boolean allowFreqs = ( ( fieldInfo . getIndexOptions ( ) . compareTo ( DOCS_AND_FREQS ) ) >= 0 ) && ( ( maxIndexOptions . compareTo ( DOCS_AND_FREQS ) ) >= 0 ) ; boolean doCheckFreqs = allowFreqs && ( ( random ( ) . nextInt ( 3 ) ) <= 2 ) ; boolean allowPositions = ( ( fieldInfo . getIndexOptions ( ) . compareTo ( DOCS_AND_FREQS_AND_POSITIONS ) ) >= 0 ) && ( ( maxIndexOptions . compareTo ( DOCS_AND_FREQS_AND_POSITIONS ) ) >= 0 ) ; boolean doCheckPositions = allowPositions && ( ( random ( ) . nextInt ( 3 ) ) <= 2 ) ; boolean allowOffsets = ( ( fieldInfo . getIndexOptions ( ) . compareTo ( DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS ) ) >= 0 ) && ( ( maxIndexOptions . compareTo ( DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS ) ) >= 0 ) ; boolean doCheckOffsets = allowOffsets && ( ( random ( ) . nextInt ( 3 ) ) <= 2 ) ; boolean doCheckPayloads = ( ( ( options . contains ( TestPostingsFormat . Option . PAYLOADS ) ) && allowPositions ) && ( fieldInfo . hasPayloads ( ) ) ) && ( ( random ( ) . nextInt ( 3 ) ) <= 2 ) ; DocsEnum prevDocsEnum = null ; DocsEnum docsEnum ; DocsAndPositionsEnum docsAndPositionsEnum ; if ( ! doCheckPositions ) { if ( allowPositions && ( ( random ( ) . nextInt ( 10 ) ) == 7 ) ) { if ( ( options . contains ( TestPostingsFormat . Option . REUSE_ENUMS ) ) && ( ( random ( ) . nextInt ( 10 ) ) < 9 ) ) { prevDocsEnum = threadState . reuseDocsAndPositionsEnum ; } int flags = 0 ; if ( random ( ) . nextBoolean ( ) ) { flags |= DocsAndPositionsEnum . FLAG_OFFSETS ; } if ( random ( ) . nextBoolean ( ) ) { flags |= DocsAndPositionsEnum . FLAG_PAYLOADS ; } if ( VERBOSE ) { System . out . println ( ( "<seq2seq4repair_space>get<seq2seq4repair_space>DocsAndPositionsEnum<seq2seq4repair_space>(but<seq2seq4repair_space>we<seq2seq4repair_space>won't<seq2seq4repair_space>check<seq2seq4repair_space>positions)<seq2seq4repair_space>flags=" + flags ) ) ; } threadState . reuseDocsAndPositionsEnum = termsEnum . docsAndPositions ( liveDocs , ( ( DocsAndPositionsEnum ) ( prevDocsEnum ) ) , flags ) ; docsEnum = threadState . reuseDocsAndPositionsEnum ; docsAndPositionsEnum = threadState . reuseDocsAndPositionsEnum ; } else { if ( VERBOSE ) { System . out . println ( "<seq2seq4repair_space>get<seq2seq4repair_space>DocsEnum" ) ; } if ( ( options . contains ( TestPostingsFormat . Option . REUSE_ENUMS ) ) && ( ( random ( ) . nextInt ( 10 ) ) < 9 ) ) { prevDocsEnum = threadState . reuseDocsEnum ; } <START_BUG> threadState . reuseDocsEnum = termsEnum . docs ( liveDocs , prevDocsEnum , doCheckFreqs ) ; <END_BUG> docsEnum = threadState . reuseDocsEnum ; docsAndPositionsEnum = null ; } } else { if ( ( options . contains ( TestPostingsFormat . Option . REUSE_ENUMS ) ) && ( ( random ( ) . nextInt ( 10 ) ) < 9 ) ) { prevDocsEnum = threadState . reuseDocsAndPositionsEnum ; } int flags = 0 ; if ( doCheckOffsets || ( ( random ( ) . nextInt ( 3 ) ) == 1 ) ) { flags |= DocsAndPositionsEnum . FLAG_OFFSETS ; } if ( doCheckPayloads || ( ( random ( ) . nextInt ( 3 ) ) == 1 ) ) { flags |= DocsAndPositionsEnum . FLAG_PAYLOADS ; } if ( VERBOSE ) { System . out . println ( ( "<seq2seq4repair_space>get<seq2seq4repair_space>DocsAndPositionsEnum<seq2seq4repair_space>flags=" + flags ) ) ; } threadState . reuseDocsAndPositionsEnum = termsEnum . docsAndPositions ( liveDocs , ( ( DocsAndPositionsEnum ) ( prevDocsEnum ) ) , flags ) ; docsEnum = threadState . reuseDocsAndPositionsEnum ; docsAndPositionsEnum = threadState . reuseDocsAndPositionsEnum ; } assertNotNull ( "null<seq2seq4repair_space>DocsEnum" , docsEnum ) ; int initialDocID = docsEnum . docID ( ) ; assertTrue ( ( "inital<seq2seq4repair_space>docID<seq2seq4repair_space>should<seq2seq4repair_space>be<seq2seq4repair_space>-1<seq2seq4repair_space>or<seq2seq4repair_space>NO_MORE_DOCS:<seq2seq4repair_space>" + docsEnum ) , ( ( initialDocID == ( - 1 ) ) || ( initialDocID == ( DocsEnum . NO_MORE_DOCS ) ) ) ) ; if ( VERBOSE ) { if ( prevDocsEnum == null ) { System . out . println ( ( "<seq2seq4repair_space>got<seq2seq4repair_space>enum=" + docsEnum ) ) ; } else if ( prevDocsEnum == docsEnum ) { System . out . println ( ( "<seq2seq4repair_space>got<seq2seq4repair_space>reuse<seq2seq4repair_space>enum=" + docsEnum ) ) ; } else { System . out . println ( ( ( ( ( "<seq2seq4repair_space>got<seq2seq4repair_space>enum=" + docsEnum ) + "<seq2seq4repair_space>(reuse<seq2seq4repair_space>of<seq2seq4repair_space>" ) + prevDocsEnum ) + "<seq2seq4repair_space>failed)" ) ) ;

Fix:
threadState . reuseDocsEnum = termsEnum . docs ( liveDocs , prevDocsEnum , ( doCheckFreqs ? DocsEnum . FLAG_FREQS : 0 ) ) ;