Buggy code:
} private static class SortingBinaryDocValues extends BinaryDocValues { private final BinaryDocValues in ; private final DocMap docMap ; SortingBinaryDocValues ( BinaryDocValues in , Sorter . DocMap docMap ) { } @ Override public void get ( int docID , BytesRef result ) { } } private static class SortingNumericDocValues extends NumericDocValues { private final NumericDocValues in ; private final DocMap docMap ; public SortingNumericDocValues ( final NumericDocValues in , Sorter . DocMap docMap ) { } @ Override public long get ( int docID ) { } } private static class SortingSortedDocValues extends SortedDocValues { private final SortedDocValues in ; private final DocMap docMap ; SortingSortedDocValues ( SortedDocValues in , Sorter . DocMap docMap ) { } @ Override public int getOrd ( int docID ) { } @ Override public void lookupOrd ( int ord , BytesRef result ) { } @ Override public int getValueCount ( ) { } @ Override public void get ( int docID , BytesRef result ) { } @ Override public int lookupTerm ( BytesRef key ) { } } private static class SortingSortedSetDocValues extends SortedSetDocValues { private final SortedSetDocValues in ; private final DocMap docMap ; SortingSortedSetDocValues ( SortedSetDocValues in , Sorter . DocMap docMap ) { } @ Override public long nextOrd ( ) { } @ Override public void setDocument ( int docID ) { } @ Override public void lookupOrd ( long ord , BytesRef result ) { } @ Override public long getValueCount ( ) { } @ Override public long lookupTerm ( BytesRef key ) { } } static class SortingDocsEnum extends FilterDocsEnum { private static final class DocFreqSorterTemplate extends SorterTemplate { private final int [ ] docs ; private final int [ ] freqs ; private int pivot ; public DocFreqSorterTemplate ( int [ ] docs , int [ ] freqs ) { } @ Override protected int compare ( int i , int j ) { } @ Override protected int comparePivot ( int j ) { } @ Override protected void setPivot ( int i ) { } @ Override protected void swap ( int i , int j ) { } } private int [ ] docs ; private int [ ] freqs ; private int docIt = - 1 ; private final int upto ; private final boolean withFreqs ; SortingDocsEnum ( SortingAtomicReader . SortingDocsEnum reuse , final DocsEnum in , boolean withFreqs , final Sorter . DocMap docMap ) throws IOException { } boolean reused ( DocsEnum other ) { } @ Override public int advance ( final int target ) throws IOException { } @ Override public int docID ( ) { return ( docIt ) < 0 ? - 1 : ( docIt ) >= ( upto ) ? NO_MORE_DOCS : docs [ docIt ] ; } @ Override public int freq ( ) throws IOException { } @ Override public int nextDoc ( ) throws IOException { } DocsEnum getWrapped ( ) { } } static class SortingDocsAndPositionsEnum extends FilterDocsAndPositionsEnum { private static final class DocOffsetSorterTemplate extends SorterTemplate { private final int [ ] docs ; private final long [ ] offsets ; private int pivot ; public DocOffsetSorterTemplate ( int [ ] docs , long [ ] offsets ) { } @ Override protected int compare ( int i , int j ) { } @ Override protected int comparePivot ( int j ) { } @ Override protected void setPivot ( int i ) { } @ Override protected void swap ( int i , int j ) { } } private int [ ] docs ; private long [ ] offsets ; private final int upto ; private final IndexInput postingInput ; private final boolean storeOffsets ; private int docIt = - 1 ; private int pos ; private int startOffset = - 1 ; private int endOffset = - 1 ; private final BytesRef payload ; private int currFreq ; private final RAMFile file ; SortingDocsAndPositionsEnum ( SortingAtomicReader . SortingDocsAndPositionsEnum reuse , final DocsAndPositionsEnum in , Sorter . DocMap docMap , boolean storeOffsets ) throws IOException { } boolean reused ( DocsAndPositionsEnum other ) { } private void addPositions ( final DocsAndPositionsEnum in , final IndexOutput out ) throws IOException { } @ Override public int advance ( final int target ) throws IOException { } @ Override public int docID ( ) { <START_BUG> return ( docIt ) >= ( upto ) ? NO_MORE_DOCS : docs [ docIt ] ; <END_BUG> } @ Override public int endOffset ( ) throws IOException { } @ Override public int freq ( ) throws IOException { } @ Override public BytesRef getPayload ( ) throws IOException { } @ Override public int nextDoc ( ) throws IOException { } @ Override public int nextPosition ( ) throws IOException { } @ Override public int startOffset ( ) throws IOException { } DocsAndPositionsEnum getWrapped ( ) { } } public static AtomicReader wrap ( AtomicReader reader , Sorter sorter ) throws IOException { } public static AtomicReader wrap ( AtomicReader reader , Sorter . DocMap docMap ) { } final DocMap docMap ; private SortingAtomicReader ( final AtomicReader in , final Sorter . DocMap docMap ) { } @ Override public void document ( final int docID , final StoredFieldVisitor visitor ) throws IOException { } @ Override public Fields fields ( ) throws IOException { } @ Override public BinaryDocValues getBinaryDocValues ( String field ) throws IOException { } @ Override public Bits getLiveDocs ( ) { } @ Override public NumericDocValues getNormValues ( String field ) throws IOException { } @ Override public NumericDocValues getNumericDocValues ( String field ) throws IOException { } @ Override public SortedDocValues getSortedDocValues ( String field ) throws IOException { } @ Override public SortedSetDocValues getSortedSetDocValues ( String field ) throws IOException { } @ Override public Fields getTermVectors ( final int docID ) throws IOException { } }

Fix:
return ( docIt ) < 0 ? - 1 : ( docIt ) >= ( upto ) ? NO_MORE_DOCS : docs [ docIt ] ;