Buggy code:
public class IMAPFolder extends AbstractRemoteFolder { private static final Logger LOG = Logger . getLogger ( "org.columba.mail.folder.imap" ) ; private IMAPServer store ; protected boolean existsOnServer = true ; private boolean readOnly ; private PersistantHeaderList headerList ; private boolean lazyFlagSync = false ; public boolean isReadOnly ( ) { } public IMAPFolder ( FolderItem folderItem , String path ) { } public IMAPFolder ( String name , String type , String path ) throws Exception { } public void removeFolder ( ) throws Exception { } public void setName ( String name ) throws Exception { } public IMAPServer getServer ( ) { } public IHeaderList getHeaderList ( ) throws Exception { } public synchronized void ensureFolderIsSynced ( boolean lazy , boolean forceSelect ) throws IOException , Exception , CommandCancelledException , IMAPException { } protected ImapItem getImapItem ( ) { } protected void printStatusMessage ( String message ) { } protected void setProgress ( int progress ) { } protected void setMaximum ( int progress ) { } private void synchronizeHeaderlist ( ) throws IOException , Exception , CommandCancelledException , IMAPException { MailboxStatus status = getServer ( ) . getStatus ( this ) ; if ( ( status . getMessages ( ) ) == 0 ) { headerList . clear ( ) ; syncMailboxInfo ( status ) ; return ; } List localUids = new LinkedList ( Arrays . asList ( headerList . getUids ( ) ) ) ; Collections . sort ( localUids ) ; int newMessages = 0 ; int largestLocalUid = ( ( localUids . size ( ) ) > 0 ) ? ( ( Integer ) ( localUids . get ( ( ( localUids . size ( ) ) - 1 ) ) ) ) . intValue ( ) : - 1 ; int largestLocalUidIndex = - 1 ; int removedLocalUids = 0 ; int largestRemoteUid = ( ( int ) ( status . getUidNext ( ) ) ) - 1 ; if ( ( ( localUids . size ( ) ) == ( status . getMessages ( ) ) ) && ( largestRemoteUid == largestLocalUid ) ) { syncMailboxInfo ( status ) ; return ; } printStatusMessage ( MailResourceLoader . getString ( "statusbar" , "message" , "sync_messages" ) ) ; if ( ( status . getMessages ( ) ) > 0 ) { largestRemoteUid = getServer ( ) . fetchUid ( new SequenceSet ( SequenceEntry . STAR ) , this ) ; if ( largestRemoteUid == ( - 1 ) ) { largestRemoteUid = getServer ( ) . fetchUid ( new SequenceSet ( status . getMessages ( ) ) , this ) ; } printStatusMessage ( MailResourceLoader . getString ( "statusbar" , "message" , "sync_messages" ) ) ; if ( ( localUids . size ( ) ) > 0 ) { largestLocalUid = ( ( Integer ) ( localUids . get ( ( ( localUids . size ( ) ) - 1 ) ) ) ) . intValue ( ) ; int position = ( localUids . size ( ) ) - 1 ; <START_BUG> while ( ( ( largestLocalUidIndex == ( - 1 ) ) && ( position >= ( ( localUids . size ( ) ) - 10 ) ) ) && ( position > 0 ) ) { <END_BUG> largestLocalUidIndex = getServer ( ) . getIndex ( ( ( Integer ) ( localUids . get ( ( position -- ) ) ) ) , this ) ; } if ( largestLocalUidIndex == ( - 1 ) ) { int a ; int b ; int c ; int index ; a = 0 ; b = position ; while ( ( b > a ) && ( ( b - a ) > 1 ) ) { c = ( Math . round ( ( ( b - a ) * 0.5F ) ) ) + a ; index = getServer ( ) . getIndex ( ( ( Integer ) ( localUids . get ( c ) ) ) , this ) ; if ( index == ( - 1 ) ) { b = c ; } else { a = c ; largestLocalUidIndex = index ; } } removedLocalUids = ( ( localUids . size ( ) ) - 1 ) - position ; } else { removedLocalUids = ( ( localUids . size ( ) ) - 2 ) - position ; } if ( largestLocalUidIndex == ( - 1 ) ) { newMessages = status . getMessages ( ) ; largestLocalUidIndex = 0 ; } else { newMessages = ( status . getMessages ( ) ) - largestLocalUidIndex ; } } else { newMessages = status . getMessages ( ) ; largestLocalUidIndex = 0 ; } if ( ( ( ( ( localUids . size ( ) ) - ( status . getMessages ( ) ) ) - removedLocalUids ) + newMessages ) < 0 ) { IMAPFolder . LOG . severe ( ( ( "Folder<seq2seq4repair_space>" + ( getName ( ) ) ) + "<seq2seq4repair_space>is<seq2seq4repair_space>out<seq2seq4repair_space>of<seq2seq4repair_space>sync<seq2seq4repair_space>-><seq2seq4repair_space>recreating<seq2seq4repair_space>the<seq2seq4repair_space>cache!" ) ) ; headerList . clear ( ) ; newMessages = status . getMessages ( ) ; largestLocalUidIndex = 0 ; largestLocalUid = - 1 ; localUids . clear ( ) ; } IMAPFolder . LOG . fine ( ( ( "Found<seq2seq4repair_space>" + newMessages ) + "<seq2seq4repair_space>new<seq2seq4repair_space>Messages" ) ) ; if ( newMessages > 0 ) { printStatusMessage ( MailResourceLoader . getString ( "statusbar" , "message" , "fetch_new_headers" ) ) ; IMAPFlags [ ] newFlags = getServer ( ) . fetchFlagsListStartFrom ( ( largestLocalUidIndex + 1 ) , this ) ; if ( ( newFlags . length ) > 0 ) { List newUids = new ArrayList ( newFlags . length ) ; for ( int i = 0 ; i < ( newFlags . length ) ;

Fix:
while ( ( ( largestLocalUidIndex == ( - 1 ) ) && ( position >= ( ( localUids . size ( ) ) - 10 ) ) ) && ( position >= 0 ) ) {