Buggy code:
public class SessionRecordV2 extends Record { private static final Object FILE_LOCK = new Object ( ) ; private static final int CURRENT_VERSION = 1 ; private final MasterSecret masterSecret ; private SessionStructure sessionStructure = SessionStructure . newBuilder ( ) . build ( ) ; public SessionRecordV2 ( Context context , MasterSecret masterSecret , RecipientDevice peer ) { } public SessionRecordV2 ( Context context , MasterSecret masterSecret , long recipientId , int deviceId ) { } private static String getRecordName ( long recipientId , int deviceId ) { } public static List < Integer > getSessionSubDevices ( Context context , CanonicalRecipient recipient ) { } public static void deleteAll ( Context context , CanonicalRecipient recipient ) { } public static void delete ( Context context , RecipientDevice recipientDevice ) { } public static boolean hasSession ( Context context , MasterSecret masterSecret , RecipientDevice recipient ) { } public static boolean hasSession ( Context context , MasterSecret masterSecret , long recipientId , int deviceId ) { } public void clear ( ) { } public void setSessionVersion ( int version ) { } public int getSessionVersion ( ) { } public void setRemoteIdentityKey ( IdentityKey identityKey ) { } public void setLocalIdentityKey ( IdentityKey identityKey ) { } public IdentityKey getRemoteIdentityKey ( ) { } public IdentityKey getLocalIdentityKey ( ) { } public int getPreviousCounter ( ) { } public void setPreviousCounter ( int previousCounter ) { } public RootKey getRootKey ( ) { } public void setRootKey ( RootKey rootKey ) { } public ECPublicKey getSenderEphemeral ( ) { } public ECKeyPair getSenderEphemeralPair ( ) { } public boolean hasReceiverChain ( ECPublicKey senderEphemeral ) { } public boolean hasSenderChain ( ) { } private Pair < Chain , Integer > getReceiverChain ( ECPublicKey senderEphemeral ) { } public ChainKey getReceiverChainKey ( ECPublicKey senderEphemeral ) { } public void addReceiverChain ( ECPublicKey senderEphemeral , ChainKey chainKey ) { } public void setSenderChain ( ECKeyPair senderEphemeralPair , ChainKey chainKey ) { } public ChainKey getSenderChainKey ( ) { } public void setSenderChainKey ( ChainKey nextChainKey ) { } public boolean hasMessageKeys ( ECPublicKey senderEphemeral , int counter ) { } public MessageKeys removeMessageKeys ( ECPublicKey senderEphemeral , int counter ) { Pair < Chain , Integer > chainAndIndex = getReceiverChain ( senderEphemeral ) ; Chain chain = chainAndIndex . first ; if ( chain == null ) { return null ; } <START_BUG> List < Chain . MessageKey > messageKeyList = chain . getMessageKeysList ( ) ; <END_BUG> Iterator < Chain . MessageKey > messageKeyIterator = messageKeyList . iterator ( ) ; MessageKeys result = null ; while ( messageKeyIterator . hasNext ( ) ) { Chain . MessageKey messageKey = messageKeyIterator . next ( ) ; if ( ( messageKey . getIndex ( ) ) == counter ) { result = new MessageKeys ( new SecretKeySpec ( messageKey . getCipherKey ( ) . toByteArray ( ) , "AES" ) , new SecretKeySpec ( messageKey . getMacKey ( ) . toByteArray ( ) , "HmacSHA256" ) , messageKey . getIndex ( ) ) ; messageKeyIterator . remove ( ) ; break ; } } Chain updatedChain = chain . toBuilder ( ) . clearMessageKeys ( ) . addAllMessageKeys ( messageKeyList ) . build ( ) ; this . sessionStructure = this . sessionStructure . toBuilder ( ) . setReceiverChains ( chainAndIndex . second , updatedChain ) . build ( ) ; return result ; } public void setMessageKeys ( ECPublicKey senderEphemeral , MessageKeys messageKeys ) { } public void setReceiverChainKey ( ECPublicKey senderEphemeral , ChainKey chainKey ) { } public void setPendingKeyExchange ( int sequence , ECKeyPair ourBaseKey , ECKeyPair ourEphemeralKey , IdentityKeyPair ourIdentityKey ) { } public int getPendingKeyExchangeSequence ( ) { } public ECKeyPair getPendingKeyExchangeBaseKey ( ) throws InvalidKeyException { } public ECKeyPair getPendingKeyExchangeEphemeralKey ( ) throws InvalidKeyException { } public IdentityKeyPair getPendingKeyExchangeIdentityKey ( ) throws InvalidKeyException { } public boolean hasPendingKeyExchange ( ) { } public void setPendingPreKey ( int preKeyId , ECPublicKey baseKey ) { } public boolean hasPendingPreKey ( ) { } public Pair < Integer , ECPublicKey > getPendingPreKey ( ) { } public void clearPendingPreKey ( ) { } public void setRemoteRegistrationId ( int registrationId ) { } public int getRemoteRegistrationId ( ) { } public void setLocalRegistrationId ( int registrationId ) { } public int getLocalRegistrationId ( ) { } public void save ( ) { } private void loadData ( ) { } }

Fix:
List < Chain . MessageKey > messageKeyList = new LinkedList < Chain . MessageKey > ( chain . getMessageKeysList ( ) ) ;