Buggy code:
public final class BytesRefHash { public static final int DEFAULT_CAPACITY = 16 ; final ByteBlockPool pool ; int [ ] bytesStart ; private final BytesRef scratch1 = new BytesRef ( ) ; private int hashSize ; private int hashHalfSize ; private int hashMask ; private int count ; private int lastCount = - 1 ; private int [ ] ords ; private final BytesRefHash . BytesStartArray bytesStartArray ; private Counter bytesUsed ; public BytesRefHash ( ) { } public BytesRefHash ( ByteBlockPool pool ) { } public BytesRefHash ( ByteBlockPool pool , int capacity , BytesRefHash . BytesStartArray bytesStartArray ) { } public int size ( ) { } public BytesRef get ( int ord , BytesRef ref ) { } public int [ ] compact ( ) { } public int [ ] sort ( final Comparator < BytesRef > comp ) { } private boolean equals ( int ord , BytesRef b ) { } private boolean shrink ( int targetSize ) { } public void clear ( boolean resetPool ) { } public void clear ( ) { } public void close ( ) { } public int add ( BytesRef bytes ) { } public int add ( BytesRef bytes , int code ) { } public int addByPoolOffset ( int offset ) { } private void rehash ( final int newSize , boolean hashOnData ) { final int newMask = newSize - 1 ; bytesUsed . addAndGet ( ( ( RamUsageEstimator . NUM_BYTES_INT ) * newSize ) ) ; final int [ ] newHash = new int [ newSize ] ; Arrays . fill ( newHash , ( - 1 ) ) ; for ( int i = 0 ; i < ( hashSize ) ; i ++ ) { final int e0 = ords [ i ] ; if ( e0 != ( - 1 ) ) { int code ; if ( hashOnData ) { final int off = bytesStart [ e0 ] ; final int start = off & ( ByteBlockPool . BYTE_BLOCK_MASK ) ; final byte [ ] bytes = pool . buffers [ ( off > > ( ByteBlockPool . BYTE_BLOCK_SHIFT ) ) ] ; code = 0 ; final int len ; int pos ; if ( ( ( bytes [ start ] ) & 128 ) == 0 ) { len = bytes [ start ] ; pos = start + 1 ; } else { len = ( ( bytes [ start ] ) & 127 ) + ( ( ( bytes [ ( start + 1 ) ] ) & 255 ) << 7 ) ; pos = start + 2 ; } final int endPos = pos + len ; while ( pos < endPos ) { <START_BUG> code = ( ( BytesRef . HASH_PRIME ) * code ) + ( bytes [ ( pos ++ ) ] ) ; <END_BUG> } } else { code = bytesStart [ e0 ] ; } int hashPos = code & newMask ; assert hashPos >= 0 ; if ( ( newHash [ hashPos ] ) != ( - 1 ) ) { final int inc = ( ( code > > 8 ) + code ) | 1 ; do { code += inc ; hashPos = code & newMask ; } while ( ( newHash [ hashPos ] ) != ( - 1 ) ) ; } newHash [ hashPos ] = e0 ; } } hashMask = newMask ; bytesUsed . addAndGet ( ( ( RamUsageEstimator . NUM_BYTES_INT ) * ( - ( ords . length ) ) ) ) ; ords = newHash ; hashSize = newSize ; hashHalfSize = newSize // 2 ; } public void reinit ( ) { } public int byteStart ( int ord ) { } @ SuppressWarnings ( "serial" ) public static class MaxBytesLengthExceededException extends RuntimeException { MaxBytesLengthExceededException ( String message ) { } } public abstract static class BytesStartArray { public abstract int [ ] init ( ) { } public abstract int [ ] grow ( ) { } public abstract int [ ] clear ( ) { } public abstract Counter bytesUsed ( ) { } } public static class TrackingDirectBytesStartArray extends BytesRefHash . BytesStartArray { protected final int initSize ; private int [ ] bytesStart ; protected final Counter bytesUsed ; public TrackingDirectBytesStartArray ( int initSize , Counter bytesUsed ) { } @ Override public int [ ] clear ( ) { } @ Override public int [ ] grow ( ) { } @ Override public int [ ] init ( ) { } @ Override public Counter bytesUsed ( ) { } } public static class DirectBytesStartArray extends BytesRefHash . BytesStartArray { protected final int initSize ; private int [ ] bytesStart ; private final Counter bytesUsed ; public DirectBytesStartArray ( int initSize ) { } @ Override public int [ ] clear ( ) { } @ Override public int [ ] grow ( ) { } @ Override public int [ ] init ( ) { } @ Override public Counter bytesUsed ( ) { } } }

Fix:
code = ( 31 * code ) + ( bytes [ ( pos ++ ) ] ) ;