Buggy code:
@ UseNoMemoryExpensiveCodec public class TestTermsEnum extends LuceneTestCase { public void test ( ) throws Exception { } private void addDoc ( RandomIndexWriter w , Collection < String > terms , Map < BytesRef , Integer > termToID , int id ) throws IOException { } private boolean accepts ( CompiledAutomaton c , BytesRef b ) { } public void testIntersectRandom ( ) throws IOException { final Directory dir = newDirectory ( ) ; final RandomIndexWriter w = new RandomIndexWriter ( random , dir ) ; <START_BUG> final int numTerms = atLeast ( 1000 ) ; <END_BUG> final Set < String > terms = new HashSet < String > ( ) ; final Collection < String > pendingTerms = new ArrayList < String > ( ) ; final Map < BytesRef , Integer > termToID = new HashMap < BytesRef , Integer > ( ) ; int id = 0 ; while ( ( terms . size ( ) ) != numTerms ) { final String s = getRandomString ( ) ; if ( ! ( terms . contains ( s ) ) ) { terms . add ( s ) ; pendingTerms . add ( s ) ; if ( ( random . nextInt ( 20 ) ) == 7 ) { addDoc ( w , pendingTerms , termToID , ( id ++ ) ) ; } } } addDoc ( w , pendingTerms , termToID , ( id ++ ) ) ; final BytesRef [ ] termsArray = new BytesRef [ terms . size ( ) ] ; final Set < BytesRef > termsSet = new HashSet < BytesRef > ( ) ; { int upto = 0 ; for ( String s : terms ) { final BytesRef b = new BytesRef ( s ) ; termsArray [ ( upto ++ ) ] = b ; termsSet . add ( b ) ; } Arrays . sort ( termsArray ) ; } if ( VERBOSE ) { System . out . println ( "\nTEST:<seq2seq4repair_space>indexed<seq2seq4repair_space>terms<seq2seq4repair_space>(unicode<seq2seq4repair_space>order):" ) ; for ( BytesRef t : termsArray ) { System . out . println ( ( ( ( "<seq2seq4repair_space>" + ( t . utf8ToString ( ) ) ) + "<seq2seq4repair_space>-><seq2seq4repair_space>id:" ) + ( termToID . get ( t ) ) ) ) ; } } final IndexReader r = w . getReader ( ) ; w . close ( ) ; final int [ ] docIDToID = DEFAULT . getInts ( r , "id" ) ; for ( int iter = 0 ; iter < ( 10 * ( RANDOM_MULTIPLIER ) ) ; iter ++ ) { final Set < String > acceptTerms = new HashSet < String > ( ) ; final TreeSet < BytesRef > sortedAcceptTerms = new TreeSet < BytesRef > ( ) ; final double keepPct = random . nextDouble ( ) ; Automaton a ; if ( iter == 0 ) { if ( VERBOSE ) { System . out . println ( "\nTEST:<seq2seq4repair_space>empty<seq2seq4repair_space>automaton" ) ; } a = BasicAutomata . makeEmpty ( ) ; } else { if ( VERBOSE ) { System . out . println ( ( "\nTEST:<seq2seq4repair_space>keepPct=" + keepPct ) ) ; } for ( String s : terms ) { final String s2 ; if ( ( random . nextDouble ( ) ) <= keepPct ) { s2 = s ; } else { s2 = getRandomString ( ) ; } acceptTerms . add ( s2 ) ; sortedAcceptTerms . add ( new BytesRef ( s2 ) ) ; } a = DaciukMihovAutomatonBuilder . build ( sortedAcceptTerms ) ; } final CompiledAutomaton c = new CompiledAutomaton ( a , true , false ) ; final BytesRef [ ] acceptTermsArray = new BytesRef [ acceptTerms . size ( ) ] ; final Set < BytesRef > acceptTermsSet = new HashSet < BytesRef > ( ) ; int upto = 0 ; for ( String s : acceptTerms ) { final BytesRef b = new BytesRef ( s ) ; acceptTermsArray [ ( upto ++ ) ] = b ; acceptTermsSet . add ( b ) ; assertTrue ( accepts ( c , b ) ) ; } Arrays . sort ( acceptTermsArray ) ; if ( VERBOSE ) { System . out . println ( "\nTEST:<seq2seq4repair_space>accept<seq2seq4repair_space>terms<seq2seq4repair_space>(unicode<seq2seq4repair_space>order):" ) ; for ( BytesRef t : acceptTermsArray ) { System . out . println ( ( ( "<seq2seq4repair_space>" + ( t . utf8ToString ( ) ) ) + ( termsSet . contains ( t ) ? "<seq2seq4repair_space>(exists)" : "" ) ) ) ; } System . out . println ( a . toDot ( ) ) ; } for ( int iter2 = 0 ; iter2 < 100 ; iter2 ++ ) { final BytesRef startTerm = ( ( ( acceptTermsArray . length ) == 0 ) || ( random . nextBoolean ( ) ) ) ? null : acceptTermsArray [ random . nextInt ( acceptTermsArray . length ) ] ; if ( VERBOSE ) { System . out . println ( ( ( ( "\nTEST:<seq2seq4repair_space>iter2=" + iter2 ) + "<seq2seq4repair_space>startTerm=" ) + ( startTerm == null ? "<null>" : startTerm . utf8ToString ( ) ) ) ) ; if ( startTerm != null ) { int state = c . runAutomaton . getInitialState ( ) ; for ( int idx = 0 ; idx < ( startTerm . length ) ; idx ++ ) { final int label = ( startTerm . bytes [ ( ( startTerm . offset ) + idx ) ] ) & 255 ; System . out . println ( ( ( ( "<seq2seq4repair_space>state=" + state ) + "<seq2seq4repair_space>label=" ) + label ) ) ; state = c . runAutomaton . step ( state , label ) ; assertTrue ( ( state != ( - 1 ) ) ) ; } System . out . println ( ( "<seq2seq4repair_space>state=" + state ) ) ; } }

Fix:
final int numTerms = atLeast ( 300 ) ;