Re: [PATCH v1 1/2] enter.c: Use wmem*() functions with wide-character strings

"Kevin J. McCarthy" <[email protected]>
Newsgroups gmane.mail.mutt.devel
Message-ID <aY6Xzkl6V9kg_TRb@qinghai>
On Thu, Feb 12, 2026 at 11:56:19PM +0100, Alejandro Colomar via Mutt-dev wrote:
>This avoids an explicit size multiplication, which can overflow the
>calculation.

My concern is the conversation in the other thread about wc support, and 
portability, and --without-wc-funcs.  Otherwise this looks fine.

>diff --git a/enter.c b/enter.c
>index 54e81178b3c5..ba8b9963659c 100644
>--- a/enter.c
>+++ b/enter.c
>@@ -29,6 +29,7 @@
> #include "buffy.h"
>
> #include <string.h>
>+#include <wchar.h>

In mutt, this is already covered by #include "mutt.h".

> /* redraw flags for mutt_enter_string() */
> enum
>@@ -175,7 +176,7 @@ static void replace_part (ENTER_STATE *state, size_t from, char *buf)
>   if (savelen)
>   {
>     savebuf = safe_calloc (savelen, sizeof (wchar_t));
>-    memcpy (savebuf, state->wbuf + state->curpos, savelen * sizeof (wchar_t));
>+    wmemcpy (savebuf, state->wbuf + state->curpos, savelen);
>   }
>
>   /* Convert to wide characters */
>@@ -191,7 +192,7 @@ static void replace_part (ENTER_STATE *state, size_t from, char *buf)
>     }
>
>     /* Restore suffix */
>-    memcpy (state->wbuf + state->curpos, savebuf, savelen * sizeof (wchar_t));
>+    wmemcpy (state->wbuf + state->curpos, savebuf, savelen);
>     FREE (&savebuf);
>   }
>
>@@ -392,7 +393,7 @@ int _mutt_enter_string (char *buf, size_t buflen, int col,
>               --i;
>             if (i)
>               --i;
>-            memmove (state->wbuf + i, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
>+            wmemmove (state->wbuf + i, state->wbuf + state->curpos, state->lastchar - state->curpos);
>             state->lastchar -= state->curpos - i;
>             state->curpos = i;
>           }
>@@ -499,7 +500,7 @@ int _mutt_enter_string (char *buf, size_t buflen, int col,
>               ++i;
>             while (i < state->lastchar && COMB_CHAR (state->wbuf[i]))
>               ++i;
>-            memmove (state->wbuf + state->curpos, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t));
>+            wmemmove (state->wbuf + state->curpos, state->wbuf + i, state->lastchar - i);
>             state->lastchar -= i - state->curpos;
>           }
>           break;
>@@ -521,8 +522,8 @@ int _mutt_enter_string (char *buf, size_t buflen, int col,
>               else
>                 --i;
>             }
>-            memmove (state->wbuf + i, state->wbuf + state->curpos,
>-                     (state->lastchar - state->curpos) * sizeof (wchar_t));
>+            wmemmove (state->wbuf + i, state->wbuf + state->curpos,
>+                      state->lastchar - state->curpos);
>             state->lastchar += i - state->curpos;
>             state->curpos = i;
>           }
>@@ -553,8 +554,8 @@ int _mutt_enter_string (char *buf, size_t buflen, int col,
>             }
>           }
>
>-          memmove (state->wbuf + state->curpos, state->wbuf + i,
>-                   (state->lastchar - i) * sizeof (wchar_t));
>+          wmemmove (state->wbuf + state->curpos, state->wbuf + i,
>+                    state->lastchar - i);
>           state->lastchar += state->curpos - i;
>           break;
>
>@@ -580,7 +581,7 @@ int _mutt_enter_string (char *buf, size_t buflen, int col,
>               ;
>             my_wcstombs (buf, buflen, state->wbuf + i, state->curpos - i);
>             if (tempbuf && templen == state->lastchar - i &&
>-                !memcmp (tempbuf, state->wbuf + i, (state->lastchar - i) * sizeof (wchar_t)))
>+                !wmemcmp (tempbuf, state->wbuf + i, state->lastchar - i))
>             {
>               mutt_select_file (buf, buflen, 0);
>               if (*buf)
>@@ -593,7 +594,7 @@ int _mutt_enter_string (char *buf, size_t buflen, int col,
>               templen = state->lastchar - i;
>               safe_realloc (&tempbuf, templen * sizeof (wchar_t));
>               if (tempbuf)
>-                memcpy (tempbuf, state->wbuf + i, templen * sizeof (wchar_t));
>+                wmemcpy (tempbuf, state->wbuf + i, templen);
>             }
>             else
>               BEEP ();
>@@ -722,7 +723,7 @@ int _mutt_enter_string (char *buf, size_t buflen, int col,
>             /* see if the path has changed from the last time */
>             if ((!tempbuf && !state->lastchar) ||
>                 (tempbuf && templen == state->lastchar &&
>-                 !memcmp (tempbuf, state->wbuf, state->lastchar * sizeof (wchar_t))))
>+                 !wmemcmp (tempbuf, state->wbuf, state->lastchar)))
>             {
>               _mutt_select_file (buf, buflen,
>                                  ((flags & MUTT_MAILBOX) ? MUTT_SEL_MAILBOX : 0) | (multiple ? MUTT_SEL_MULTI : 0),
>@@ -751,7 +752,7 @@ int _mutt_enter_string (char *buf, size_t buflen, int col,
>               templen = state->lastchar;
>               safe_realloc (&tempbuf, templen * sizeof (wchar_t));
>               if (tempbuf)
>-                memcpy (tempbuf, state->wbuf, templen * sizeof (wchar_t));
>+                wmemcpy (tempbuf, state->wbuf, templen);
>             }
>             else
>               BEEP (); /* let the user know that nothing matched */
>@@ -875,7 +876,7 @@ self_insert:
>           state->wbuflen = state->lastchar + 20;
>           safe_realloc (&state->wbuf, state->wbuflen * sizeof (wchar_t));
>         }
>-        memmove (state->wbuf + state->curpos + 1, state->wbuf + state->curpos, (state->lastchar - state->curpos) * sizeof (wchar_t));
>+        wmemmove (state->wbuf + state->curpos + 1, state->wbuf + state->curpos, state->lastchar - state->curpos);
>         state->wbuf[state->curpos++] = wc;
>         state->lastchar++;
>       }
>-- 
>2.51.0
>

-- 
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-----

iQIzBAEBCgAdFiEEiXWpszqjeRA4XFMIre92hIAxa9oFAmmOl84ACgkQre92hIAx
a9p2BxAAoEMbKypj6BLwrx3hmrNJLeMiRK7iUN3p4pTggueU3jjyOXoK304VLyQB
2ay7mTCeV1elsYYKtvpAAyX8KSB+VorREqf3DDR2Q1xWXuiX9OP2ESCJDewqstUT
VZXzQZkoRfBw73pl5BTD7drGclm8/yxxXzA5PksL3TjWrEESPwal+EyFwmr7IzYU
CtgEgrQgubdRxYndKx0BqRyFTtUwBDBFpgKxrFjeGuXpBi9LX1LPrveJW31b9nmh
uD8Tv//lhbU+x6wTY7Dv/5KEwqPbrA5PHh/rK07NtxMdcTpLv7JivcXdXLFk+Cq8
4Q6AO3DLmW8ZTkj1Lleoqtqj1J8tqB87EXKYludfnY7LzsrthnDy1SV05hX+5fVx
D+PxcghjJ8Ly8As0IEHLysFk8/dHnsWpyxg2pgj86Kx1FsDbIm+yk2d9K8JRae0J
cBipFKjbu8jBMtFAJXLDFDJbdqwHKhttcJiFIkLWfmqUMxSv3yu01iFbPMEtxqpK
tkO9mbsGfBVx1A54gqp/clyNnOHMMwxjiAYr3iewTzhD/Ywj++LlmZPCaAEhDm4h
pRF9GHiFeAvEYpEPXZuXE4K64ypMOJyvloKURGWaVkTrYoyiDGkkTQFsD0USBgBJ
gq1QEKCqfXgnn0/A+EEOVoKNiyAWesq8d2qc82V6acXRKvcjhDE=
=zrdV
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.