Re: [PATCH] Search for keys by uid using utf-8.

Alejandro Colomar via Mutt-dev <[email protected]> Mon, 18 May 2026 13:03:29 +0200
Newsgroups gmane.mail.mutt.devel
Message-ID <agrvauru1hul7JTc@devuan>
Hi Kevin,

On 2026-05-18T18:34:25+0800, Kevin J. McCarthy wrote:
> +static char *intl_to_utf8(char *orig_user, char *orig_domain)
> +{
> +  char *utf8_user, *utf8_domain, *mailbox = NULL, *tmp = NULL;
> +
> +  utf8_user = orig_user;
> +  utf8_domain = safe_strdup(orig_domain);
> +
> +#if defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2)
> +  /* NOTE: since this transform is currently used for key lookups,
> +   * the check for option(OPTIDNDECODE) is not included here.
> +   * Compare to the invocation in intl_to_local()
> +   */
> +  if (check_idn(utf8_domain))
> +  {
> +    if (idna_to_unicode_8z8z(utf8_domain, &tmp, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
> +      goto cleanup;
> +    mutt_str_replace(&utf8_domain, tmp);
> +  }
> +#endif
> +
> +  mailbox = safe_malloc(mutt_strlen(utf8_user) + mutt_strlen(utf8_domain) + 2);
> +  sprintf(mailbox, "%s@%s", NONULL(utf8_user), NONULL(utf8_domain)); /* __SPRINTF_CHECKED__ */

Should we use an sprintf(3) variant that allocates itself?  That could
simplify this to:

	mailbox = aprintf("%s@%s", NONULL(utf8_user), NONULL(utf8_domain));
	if (mailbox == NULL)
		goto fail;

It would make it impossible to calculate the size of the buffer
incorrectly, or any other misuses.

Also, we could have a wrapper that fails on OOM, as we do with
malloc(3):

	mailbox = xaprintf("%s@%s", NONULL(utf8_user), NONULL(utf8_domain));

Which doesn't need error handling code.

I'm working on a patch for adding aprintf(3) in glibc (we've already
added it in gnulib, and it already has a manual page).  It's also quite
easy to implement it portably with C99 functions.  Here's a paper I
wrote, which specifies the function, and shows a portable
implementation:
<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3750.txt>

	char *
	vaprintf(const char *restrict fmt, va_list ap)
	{
		char     *p;
		size_t   size, len;
		va_list  ap2;

		va_copy(ap2, ap);
		len = vsnprintf(NUL, 0, fmt, ap);
		va_end(ap2);
		if (len < 0)
			return NULL;

		size = len + 1;
		p = malloc(size);
		if (p == NULL)
			return NULL;

		if (vsnprintf(p, size, fmt, ap) < 0) {
			free(p);
			return NULL;
		}

		return p;
	}


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmoK8fsACgkQ64mZXMKQ
wqnDShAAlS0M6aQRRd1EvFIDryt1B/pUgJ+/MKXt9XSWL50YM22uFQOB4jG4x+X0
T+gUf5dmAKVaduF7ABBaasRPm+lEs+5wWREWAmlEo8SVN/izZXRKTSF09pdq7D27
N3QE4b9A3GpqrRVxQ4efq8++G422X9JDgbP7HsaXxjpAZ/nFShhjJ11ntsgvPlF9
wXWhGAB9bAmwCh2t+r5SRAGhxAUUzpjxXmzrSjyJ01UpYNGuwZWLeAIgGJn6tyQV
KBFG2GDmb2AXHw/SF4SrDudmzWc4QfNltOCPJ7I8ywBS74Y0iYA759nGE5/Tth3c
a/MPNmc38EUTJ8mIsylTkw6sumOHcUUSs4AHtiucZFYsmMTmnhf3ZfSW2c92HIKl
Ujx2RNGulCKYFHHU6epPo2sSgiWr9Ql++J4TLQYI9Fvcm1PXHNMocpEZV/SZY9KT
Q8Dz28NFXQ8Z7qdkSz+k6ZOGeww6RpHlFM27SiqALt/jTR4BfCSPeQKg0J9bWwyc
kPpqsykDMvGjHQKWsdLSdNtGZhngQpdoZzVUMQa05MTqfLa1jLhciXZ9bWwXl+1q
Zk4hMIFAzQocXvNb85s+njLdoDbM66ErDtHhhe7qO8fWFqeQMivTQ6q14QwCkVJw
7GoBddbpIqPjmIAC1QgzH1lZB4/AUuiJMaxPcpIsTArmGGLkijs=
=ozTw
-----END PGP SIGNATURE-----