changeset: 6609:a8bc75e7c4e1 user: Kevin McCarthy <ke...@8t8.us> date: Fri Apr 08 15:20:53 2016 -0700 link: http://dev.mutt.org/hg/mutt/rev/a8bc75e7c4e1
Reset mbstate for other mbrtowc() calls returning -1 Continue the cleanup started in changesets c8c76a6a1e61 and a3450fd50d11. In those changesets, a bug was occurring due to the mbstate not being reset when mbrtowc() returned -1. This patch fixes other callers of mbrtowc() to reset mbstate when it returns -1. changeset: 6610:180a90d05ed4 user: Kevin McCarthy <ke...@8t8.us> date: Fri Apr 08 15:27:17 2016 -0700 link: http://dev.mutt.org/hg/mutt/rev/180a90d05ed4 Fix pager.c format_line() to use size_t for mbrtowc() retvals. While fixing up the error checking for mbrtowc(), I noticed the uses in pager.c format_line() were assigning the retval to an int. The cleanup for this was a little tricky, because it was making use of possible negative values for (k1 - k). The backspace detection loop condition was a bit heavy, so this patch first pulled the initialization and first call above, and put the second call inside the loop. Note that k1 previously included k, but this patch changes it to be just the retval of mbrtowc. This means the second mbrtowc() arguments are changed to include k, as is the ch increment at the bottom of the loop. diffs (126 lines): diff -r 8251a4d42656 -r 180a90d05ed4 alias.c --- a/alias.c Wed Apr 06 09:58:06 2016 -0700 +++ b/alias.c Fri Apr 08 15:27:17 2016 -0700 @@ -423,6 +423,8 @@ { if (dry) return -1; + if (l == (size_t)(-1)) + memset (&mb, 0, sizeof (mbstate_t)); *dest++ = '_'; rv = -1; } diff -r 8251a4d42656 -r 180a90d05ed4 curs_lib.c --- a/curs_lib.c Wed Apr 06 09:58:06 2016 -0700 +++ b/curs_lib.c Fri Apr 08 15:27:17 2016 -0700 @@ -1034,6 +1034,8 @@ { if (k == (size_t)(-1) || k == (size_t)(-2)) { + if (k == (size_t)(-1)) + memset (&mbstate, 0, sizeof (mbstate)); k = (k == (size_t)(-1)) ? 1 : n; wc = replacement_char (); } diff -r 8251a4d42656 -r 180a90d05ed4 help.c --- a/help.c Wed Apr 06 09:58:06 2016 -0700 +++ b/help.c Fri Apr 08 15:27:17 2016 -0700 @@ -103,6 +103,8 @@ { if (k == (size_t)(-1) || k == (size_t)(-2)) { + if (k == (size_t)(-1)) + memset (&mbstate1, 0, sizeof (mbstate1)); k = (k == (size_t)(-1)) ? 1 : len; wc = replacement_char (); } @@ -165,6 +167,8 @@ m = n; if (k == (size_t)(-1) || k == (size_t)(-2)) { + if (k == (size_t)(-1)) + memset (&mbstate, 0, sizeof (mbstate)); k = (k == (size_t)(-1)) ? 1 : len; wc = replacement_char (); } diff -r 8251a4d42656 -r 180a90d05ed4 pager.c --- a/pager.c Wed Apr 06 09:58:06 2016 -0700 +++ b/pager.c Fri Apr 08 15:27:17 2016 -0700 @@ -1011,10 +1011,14 @@ for (; len > 0; buf += k, len -= k) { k = mbrtowc (NULL, (char *) buf, len, &mbstate); - if (k == -2) + if (k == (size_t)(-2)) break; - else if (k == -1 || k == 0) + else if (k == (size_t)(-1) || k == 0) + { + if (k == (size_t)(-1)) + memset (&mbstate, 0, sizeof (mbstate)); k = 1; + } } *buf = '\0'; @@ -1092,7 +1096,8 @@ { int space = -1; /* index of the last space or TAB */ int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0; - int ch, vch, k, last_special = -1, special = 0, t; + size_t k; + int ch, vch, last_special = -1, special = 0, t; wchar_t wc; mbstate_t mbstate; int wrap_cols = mutt_term_width ((flags & M_PAGER_NOWRAP) ? 0 : Wrap); @@ -1123,8 +1128,10 @@ break; k = mbrtowc (&wc, (char *)buf+ch, cnt-ch, &mbstate); - if (k == -2 || k == -1) + if (k == (size_t)(-2) || k == (size_t)(-1)) { + if (k == (size_t)(-1)) + memset(&mbstate, 0, sizeof(mbstate)); dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n", __FILE__, __LINE__, k, errno)); if (col + 4 > wrap_cols) @@ -1150,15 +1157,18 @@ { wchar_t wc1; mbstate_t mbstate1; - int k1, k2; + size_t k1, k2; - while ((wc1 = 0, mbstate1 = mbstate, - k1 = k + mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1), - k1 - k > 0 && wc1 == '\b') && - (wc1 = 0, - k2 = mbrtowc (&wc1, (char *)buf+ch+k1, cnt-ch-k1, &mbstate1), - k2 > 0 && IsWPrint (wc1))) + mbstate1 = mbstate; + k1 = mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1); + while ((k1 != (size_t)(-2)) && (k1 != (size_t)(-1)) && + (k1 > 0) && (wc1 == '\b')) { + k2 = mbrtowc (&wc1, (char *)buf+ch+k+k1, cnt-ch-k-k1, &mbstate1); + if ((k2 == (size_t)(-2)) || (k2 == (size_t)(-1)) || + (k2 == 0) || (!IsWPrint (wc1))) + break; + if (wc == wc1) { special |= (wc == '_' && special & A_UNDERLINE) @@ -1174,9 +1184,11 @@ /* special = 0; / * overstrike: nothing to do! */ wc = wc1; } - ch += k1; + + ch += k + k1; k = k2; mbstate = mbstate1; + k1 = mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1); } }