Index: sys/kern/tty_ttydisc.c =================================================================== --- sys/kern/tty_ttydisc.c +++ sys/kern/tty_ttydisc.c @@ -844,6 +844,7 @@ */ ttyinq_write(&tp->t_inq, bytes, UTF8_STACKBUF, 0); + ttyinq_unputchar(&tp->t_inq); } else { /* Find codepoint and width. */ codepoint = @@ -861,6 +862,7 @@ */ ttyinq_write(&tp->t_inq, bytes, nb, 0); + ttyinq_unputchar(&tp->t_inq); } } tp->t_column -= cwidth; Index: sys/teken/teken_wcwidth.h =================================================================== --- sys/teken/teken_wcwidth.h +++ sys/teken/teken_wcwidth.h @@ -128,8 +128,11 @@ teken_utf8_bytes_to_codepoint(uint8_t bytes[4], int nbytes) { - /* Check for malformed characters. */ - if (bitcount(bytes[0] & 0xf0) != nbytes) + /* + * Check for malformed characters by comparing 'nbytes' to + * the number of consecutive bits set starting from the MSB. + */ + if (__builtin_clz(~(bytes[0] & 0xf0) << 24) != nbytes) return (TEKEN_UTF8_INVALID_CODEPOINT); switch (nbytes) {