=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tmux/utf8.c,v retrieving revision 1.20 retrieving revision 1.21 diff -c -r1.20 -r1.21 *** src/usr.bin/tmux/utf8.c 2015/11/13 08:09:28 1.20 --- src/usr.bin/tmux/utf8.c 2015/11/14 10:56:31 1.21 *************** *** 1,4 **** ! /* $OpenBSD: utf8.c,v 1.20 2015/11/13 08:09:28 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott --- 1,4 ---- ! /* $OpenBSD: utf8.c,v 1.21 2015/11/14 10:56:31 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott *************** *** 403,424 **** /* * Append character to UTF-8, closing if finished. * ! * Returns 1 if more UTF-8 data to come, 0 if finished. */ int utf8_append(struct utf8_data *ud, u_char ch) { - /* XXX this should do validity checks too! */ - if (ud->have >= ud->size) fatalx("UTF-8 character overflow"); if (ud->size > sizeof ud->data) fatalx("UTF-8 character size too large"); ud->data[ud->have++] = ch; if (ud->have != ud->size) return (1); ud->width = utf8_width(utf8_combine(ud)); return (0); } --- 403,428 ---- /* * Append character to UTF-8, closing if finished. * ! * Returns 1 if more UTF-8 data to come, 0 if finished and valid, -1 if ! * finished and invalid. */ int utf8_append(struct utf8_data *ud, u_char ch) { if (ud->have >= ud->size) fatalx("UTF-8 character overflow"); if (ud->size > sizeof ud->data) fatalx("UTF-8 character size too large"); + if (ud->have != 0 && (ch & 0xc0) != 0x80) + ud->width = 0xff; + ud->data[ud->have++] = ch; if (ud->have != ud->size) return (1); + if (ud->width == 0xff) + return (-1); ud->width = utf8_width(utf8_combine(ud)); return (0); } *************** *** 556,570 **** while (src < end) { if (utf8_open(&ud, *src)) { more = 1; ! while (++src < end && more) more = utf8_append(&ud, *src); ! if (!more) { /* UTF-8 character finished. */ for (i = 0; i < ud.size; i++) *dst++ = ud.data[i]; continue; } else if (ud.have > 0) { ! /* Not a complete UTF-8 character. */ src -= ud.have; } } --- 560,574 ---- while (src < end) { if (utf8_open(&ud, *src)) { more = 1; ! while (++src < end && more == 1) more = utf8_append(&ud, *src); ! if (more == 0) { /* UTF-8 character finished. */ for (i = 0; i < ud.size; i++) *dst++ = ud.data[i]; continue; } else if (ud.have > 0) { ! /* Not a complete, valid UTF-8 character. */ src -= ud.have; } } *************** *** 600,608 **** dst = xreallocarray(dst, n + 1, sizeof *dst); if (utf8_open(&ud, *src)) { more = 1; ! while (*++src != '\0' && more) more = utf8_append(&ud, *src); ! if (!more) { dst = xreallocarray(dst, n + ud.width, sizeof *dst); for (i = 0; i < ud.width; i++) --- 604,612 ---- dst = xreallocarray(dst, n + 1, sizeof *dst); if (utf8_open(&ud, *src)) { more = 1; ! while (*++src != '\0' && more == 1) more = utf8_append(&ud, *src); ! if (more != 1) { dst = xreallocarray(dst, n + ud.width, sizeof *dst); for (i = 0; i < ud.width; i++) *************** *** 612,621 **** src -= ud.have; } if (*src > 0x1f && *src < 0x7f) ! dst[n] = *src; src++; - - n++; } dst = xreallocarray(dst, n + 1, sizeof *dst); --- 616,623 ---- src -= ud.have; } if (*src > 0x1f && *src < 0x7f) ! dst[n++] = *src; src++; } dst = xreallocarray(dst, n + 1, sizeof *dst); *************** *** 641,658 **** dst = xreallocarray(dst, n + 1, sizeof *dst); if (utf8_open(&dst[n], *src)) { more = 1; ! while (*++src != '\0' && more) more = utf8_append(&dst[n], *src); ! if (!more) { n++; continue; } src -= dst[n].have; } ! utf8_set(&dst[n], *src); src++; - - n++; } dst = xreallocarray(dst, n + 1, sizeof *dst); --- 643,661 ---- dst = xreallocarray(dst, n + 1, sizeof *dst); if (utf8_open(&dst[n], *src)) { more = 1; ! while (*++src != '\0' && more == 1) more = utf8_append(&dst[n], *src); ! if (more != 1) { n++; continue; } src -= dst[n].have; } ! if (*src > 0x1f && *src < 0x7f) { ! utf8_set(&dst[n], *src); ! n++; ! } src++; } dst = xreallocarray(dst, n + 1, sizeof *dst); *************** *** 693,707 **** while (*s != '\0') { if (utf8_open(&tmp, *s)) { more = 1; ! while (*++s != '\0' && more) more = utf8_append(&tmp, *s); ! if (!more) { width += tmp.width; continue; } s -= tmp.have; } ! width++; s++; } return (width); --- 696,711 ---- while (*s != '\0') { if (utf8_open(&tmp, *s)) { more = 1; ! while (*++s != '\0' && more == 1) more = utf8_append(&tmp, *s); ! if (more != 1) { width += tmp.width; continue; } s -= tmp.have; } ! if (*s > 0x1f && *s < 0x7f) ! width++; s++; } return (width);