From: Karel Zak Date: Mon, 21 Mar 2016 00:06:16 +0000 (-0700) Subject: Improve error handling in mutt_wstr_trunc(). X-Git-Tag: neomutt-20160404~38 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=337b98bab2b8bbb705ae1083b4009a99b91ce5a4;p=neomutt Improve error handling in mutt_wstr_trunc(). This is Karel Zak's patch to fix handling of (illegal) multi-byte chars. * mutt_wstr_trunc(): Reset mbstate after error in mbrtowc(). Set wc=0 if wcwidth returns < 0. Addresses: https://github.com/karelzak/mutt-kz/issues/58 Thanks to Richard Russon for bringing this patch to our attention. --- diff --git a/curs_lib.c b/curs_lib.c index 7a1131f7d..b400db0ae 100644 --- a/curs_lib.c +++ b/curs_lib.c @@ -964,8 +964,10 @@ int mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *widt memset (&mbstate, 0, sizeof (mbstate)); for (w = 0; n && (cl = mbrtowc (&wc, src, n, &mbstate)); src += cl, n -= cl) { - if (cl == (size_t)(-1) || cl == (size_t)(-2)) + if (cl == (size_t)(-1) || cl == (size_t)(-2)) { cw = cl = 1; + memset(&mbstate, 0, sizeof (mbstate)); + } else { cw = wcwidth (wc); @@ -973,6 +975,8 @@ int mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *widt * until rendered by print_enriched_string (#3364) */ if (cw < 0 && cl == 1 && src[0] && src[0] < M_TREE_MAX) cw = 1; + else if (cw < 0) + cw = 0; /* unprintable wchar */ } if (cl + l > maxlen || cw + w > maxwid) break;