Re: Fwd: report 3/3: latent unterminated buffer in imap_wordcasecmp()

"Kevin J. McCarthy" <[email protected]> Fri, 19 Jun 2026 13:02:17 +0800
Newsgroups gmane.mail.mutt.devel
Message-ID <[email protected]>
On Thu, Jun 18, 2026 at 02:46:50PM +0200, Alejandro Colomar via Mutt-dev wrote:
>On 2026-06-18T14:43:52+0200, Alejandro Colomar wrote:
>> TBH, it still reads a bit too complex to me.  I struggle to fully
>> understand it.
>>
>> I've tried rewriting the function from scratch, to see if I understand
>> it correctly.  Is this rewrite correct?
>>
>> 	@@ -884,20 +884,10 @@ void imap_unmunge_mbox_name(IMAP_DATA *idata, char *s)
>> 	 int imap_wordcasecmp(const char *a, const char *b)
>> 	 {
>> 	   char tmp[SHORT_STRING];
>> 	-  const char *s = b;
>> 	-  int i;
>> 	
>> 	-  tmp[SHORT_STRING-1] = 0;
>> 	-  for (i=0;i < SHORT_STRING-2;i++,s++)
>> 	-  {
>> 	-    if (!*s || IS_ASCII_WS(*s))
>> 	-    {
>> 	-      tmp[i] = 0;
>> 	-      break;
>> 	-    }
>> 	-    tmp[i] = *s;
>> 	-  }
>> 	-  tmp[i+1] = 0;
>> 	+  len = strspn(b, " \t\n\v\f\r");  // We could #define ASCII_WS " \t\n\v\f\r"
>
>Actually, I meant strcspn().
>
>And I forgot to declare len as size_t.
>
>> 	+  len = MIN(len, sizeof(tmp)-1);  // Is truncation a problem?!
>> 	+  strcpy(mempcpy(tmp, b, len), "");

Seems okay for (fixing and ) duplicating the functionality.

Currently, truncation of b isn't a problem because all of the "a" 
parameters are in the Capabilities array, and aren't even close to the 
size SHORT_STRING, so any "b" value of size 127 is not going to match an 
"a" parameter.

However, this "utility" function is pretty lousy, and as Ian mentions I 
can't think of any reason to copy b over to a tmp[] array.

If we're going to rewrite it, why don't we just compute length of a, 
grab the strcpn() retval as length of b, and if they match then just use 
ascii_strncmp()?

-- 
Kevin J. McCarthy
GPG Fingerprint: 8975 A9B3 3AA3 7910 385C  5308 ADEF 7684 8031 6BDA
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEiXWpszqjeRA4XFMIre92hIAxa9oFAmo0zVkACgkQre92hIAx
a9oydA/7BgugrnpGpkd3o4tYPQzmNopJE8CFXlN8/8dfqPCdWeLgfMnWQQ4uqc24
0JRDL+M8tdoBHn6jd1oY15Uq0j4cErsdH1CM0u1JfukBA6Urm5LT3xsAeFiYjldx
kZwDP5YCRfmZTjfG2HlNZQp8sFL6A9JAWhzypucMcwJgaV3rWM3a3sGF5tdA6/Rr
FsytYn8cUdecC4l318BtHc83gyP2dVcsmXHFgdzGLRObYgKdaQjrlzDR3pzUXhnd
epMKmid/8NhrX7DF089jA7OutWViAjITSTfJ8PbAc9lphOu9AmDn7yidmlb3Vfz0
0Ce3UwWfBqmZDbKT7TAmNSbVbT2JaW5D40CayHznv7Tprx7R5Lmnt9mXNdxgAq6k
Y29dGOZOx0BUcNv1FN8/rDF8KcjTb4mMZdDIbTz57GZP3b6x0NREHcV4FW6BrACn
/jggMRQQ4wovcSTh4dFg27pd5pHhzSaQHH+/jDEM32zQ1Qn1k4dfFHuvFC5/7ATU
FmVHpJiV3sY2+RwkEJRjWHXtXXwYdLZj0Y53Qw/b0aCqW68QjF3dbqkgGCMUBCRx
5eduK1caDSh3IaYO18wzen0Tlxp9BEGhmVDiQ4U8XMetPJ+qTsrTz02bk/M9qgni
IGNlWwI6R6WiiSNTNJF7h8Q0DBacMQM7YzAX1CCiVxzZaozTcQs=
=MdA8
-----END PGP SIGNATURE-----