Re: The SORT extension in 2.4.4 over imapc never returns any UIDs

Timo Sirainen via dovecot <[email protected]> Mon, 29 Jun 2026 15:21:23 +0300
Newsgroups gmane.mail.imap.dovecot
Message-ID <[email protected]>
--===============8260572605666545815==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_8BC6819F-673B-47A2-ADA5-683E7910D3BE"


--Apple-Mail=_8BC6819F-673B-47A2-ADA5-683E7910D3BE
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

On 27. Jun 2026, at 0.25, inaugurator--- via dovecot =
<[email protected]> wrote:
>=20
> Hi guys!
>=20
> This is really annoying. On the command to dovecot over imapc from =
another one:
> "UID SEARCH ALL"
> I get my UIDs:
> "* SEARCH 1 2 3 4"
> But when sending:
> "UID SORT (REVERSE ARRIVAL) US-ASCII NOT DELETED"
> I get literally nothing:
> "* SORT"
>=20
> Sorry, but this is a bug.

This is a regression caused by =
https://github.com/dovecot/core/commit/97adde09653753ec9269d34d17a051585bd=
2ce05

Attached a fix.

=EF=BF=BC


--Apple-Mail=_8BC6819F-673B-47A2-ADA5-683E7910D3BE
Content-Type: multipart/mixed;
	boundary="Apple-Mail=_F1EE52DB-4D02-45A8-89F0-9E1C37D36FE3"


--Apple-Mail=_F1EE52DB-4D02-45A8-89F0-9E1C37D36FE3
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"

   On 27. Jun 2026, at 0.25, inaugurator--- via dovecot <[email protected]>
   wrote:

     Hi guys!

     This is really annoying. On the command to dovecot over imapc from
     another one:
     "UID SEARCH ALL"
     I get my UIDs:
     "* SEARCH 1 2 3 4"
     But when sending:
     "UID SORT (REVERSE ARRIVAL) US-ASCII NOT DELETED"
     I get literally nothing:
     "* SORT"

     Sorry, but this is a bug.

   This is a regression caused
   by [1]https://github.com/dovecot/core/commit/97adde09653753ec9269d34d17a051585bd2ce05
   Attached a fix.

References

   Visible links
   1. https://github.com/dovecot/core/commit/97adde09653753ec9269d34d17a051585bd2ce05

--Apple-Mail=_F1EE52DB-4D02-45A8-89F0-9E1C37D36FE3
Content-Disposition: attachment;
	filename=imapc-sort-fix.patch
Content-Type: application/octet-stream;
	x-unix-mode=0640;
	name="imapc-sort-fix.patch"
Content-Transfer-Encoding: 7bit

commit 2feeca740df61f4131c81185a500ff573890c5ac
Author: Timo Sirainen <[email protected]>
Date:   Mon Jun 29 11:39:17 2026 +0000

    imapc: Fix SORT passthrough returning empty result with search criteria
    
    UID SORT with search criteria (e.g. "UID SORT (REVERSE ARRIVAL) US-ASCII
    NOT DELETED") returned an empty "* SORT" reply over imapc, even though
    "UID SEARCH" with the same criteria returned the expected UIDs.
    
    The SORT reply handler stored the remote UIDs and then mapped them to
    sequences via the local index (mail_index_lookup_seq). The local index
    isn't necessarily populated yet - for example right after SELECT - so
    every lookup failed and all messages were dropped. It also didn't mark
    the search args as matched, so they got re-evaluated against local mail
    that may not be available.
    
    Map the remote UIDs through the live msgmap and mark the args as matched,
    the same way the SEARCH passthrough does.
    
    Broken by 97adde09653753ec9269d34d17a051585bd2ce05

diff --git a/src/lib-storage/index/imapc/imapc-search.c b/src/lib-storage/index/imapc/imapc-search.c
index c51b870e03..583c534074 100644
--- a/src/lib-storage/index/imapc/imapc-search.c
+++ b/src/lib-storage/index/imapc/imapc-search.c
@@ -362,10 +362,26 @@ bool imapc_search_next_update_seq(struct mail_search_context *ctx)
 		return index_storage_search_next_update_seq(ctx);
 
 	if (ictx->sorted) {
+		struct imapc_mailbox *mbox =
+			IMAPC_MAILBOX(ctx->transaction->box);
+		struct imapc_msgmap *msgmap =
+			imapc_client_mailbox_get_msgmap(mbox->client_box);
+		uint32_t rseq;
+
+		/* convert remote UIDs to sequences via the live msgmap, the
+		   same way the SEARCH reply does. The local index may not yet
+		   contain these messages (e.g. just after SELECT), so looking
+		   them up from there would drop them and return nothing. */
 		while (ictx->n < array_count(&ictx->sorted_uids)) {
 			uidp = array_idx(&ictx->sorted_uids, ictx->n++);
-			if (mail_index_lookup_seq(ctx->transaction->view, *uidp, &ctx->seq))
+			if (imapc_msgmap_uid_to_rseq(msgmap, *uidp, &rseq)) {
+				ctx->seq = rseq;
+				ctx->progress_cur = ctx->seq;
+				/* the remote already evaluated the search args -
+				   don't re-evaluate them against local mail. */
+				imapc_search_set_matches(ctx->args->args);
 				return TRUE;
+			}
 		}
 		return FALSE;
 	}

--Apple-Mail=_F1EE52DB-4D02-45A8-89F0-9E1C37D36FE3
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"


--Apple-Mail=_F1EE52DB-4D02-45A8-89F0-9E1C37D36FE3--

--Apple-Mail=_8BC6819F-673B-47A2-ADA5-683E7910D3BE--

--===============8260572605666545815==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
dovecot mailing list -- [email protected]
To unsubscribe send an email to [email protected]

--===============8260572605666545815==--