Index: dovecot-2.2.13/src/pop3/pop3-client.c =================================================================== --- dovecot-2.2.13/src/pop3/pop3-client.c +++ dovecot-2.2.13/src/pop3/pop3-client.c 2015-11-17 19:12:46.817117794 +0100 @@ -194,6 +194,9 @@ client->last_seen_pop3_msn = msgnum + 1; client->total_size += size; + if (client->highest_seq < mail->seq) + client->highest_seq = mail->seq; + array_append(&message_sizes, &size, 1); msgnum++; } Index: dovecot-2.2.13/src/pop3/pop3-client.h =================================================================== --- dovecot-2.2.13/src/pop3/pop3-client.h +++ dovecot-2.2.13/src/pop3/pop3-client.h 2015-11-17 19:16:15.267610508 +0100 @@ -68,6 +68,7 @@ /* All sequences currently visible in the mailbox. */ ARRAY_TYPE(seq_range) all_seqs; + uint32_t highest_seq; /* [msgnum] contains mail seq. anything after it has seq = msgnum+1 */ uint32_t *msgnum_to_seq_map; Index: dovecot-2.2.13/src/pop3/pop3-commands.c =================================================================== --- dovecot-2.2.13/src/pop3/pop3-commands.c +++ dovecot-2.2.13/src/pop3/pop3-commands.c 2015-11-17 19:19:22.918645918 +0100 @@ -794,7 +794,7 @@ client->uidl_pool = pool_alloconly_create("message uidls", 1024); /* first read all the UIDLs into a temporary [seq] array */ - seq_uidls = i_new(const char *, client->messages_count); + seq_uidls = i_new(const char *, client->highest_seq); str = t_str_new(128); while (mailbox_search_next(search_ctx, &mail)) { str_truncate(str, 0); @@ -809,6 +809,7 @@ if (client->set->pop3_save_uidl && !permanent_uidl) mail_update_pop3_uidl(mail, uidl); + i_assert(mail->seq <= client->highest_seq); seq_uidls[mail->seq-1] = uidl; hash_table_insert(prev_uidls, uidl, POINTER_CAST(1)); }