summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2016-03-02 15:36:02 +0000
committernicm <nicm@openbsd.org>2016-03-02 15:36:02 +0000
commit98da63d5a13e2347fd3691b3336d5820e8c25d9b (patch)
treebfae7f5c71c92e0c3363071046c3ef57b6450d3f
parentLimit x, y and b to 0x7ff for UTF-8 mouse input, suggested by schwarze@. (diff)
downloadwireguard-openbsd-98da63d5a13e2347fd3691b3336d5820e8c25d9b.tar.xz
wireguard-openbsd-98da63d5a13e2347fd3691b3336d5820e8c25d9b.zip
Handle wcwidth() and mbtowc() failures in better style and drop
characters where we can't find the width (wcwidth() fails) on input, the same as we drop invalid UTF-8. Suggested by schwarze@.
-rw-r--r--usr.bin/tmux/input.c12
-rw-r--r--usr.bin/tmux/key-string.c8
-rw-r--r--usr.bin/tmux/tmux.h5
-rw-r--r--usr.bin/tmux/tty-keys.c9
-rw-r--r--usr.bin/tmux/utf8.c45
5 files changed, 53 insertions, 26 deletions
diff --git a/usr.bin/tmux/input.c b/usr.bin/tmux/input.c
index 8fdf1383c92..bfb2b6fb855 100644
--- a/usr.bin/tmux/input.c
+++ b/usr.bin/tmux/input.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: input.c,v 1.100 2016/01/29 11:13:56 nicm Exp $ */
+/* $OpenBSD: input.c,v 1.101 2016/03/02 15:36:02 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -1960,8 +1960,14 @@ input_utf8_close(struct input_ctx *ictx)
{
struct utf8_data *ud = &ictx->utf8data;
- if (utf8_append(ud, ictx->ch) != UTF8_DONE)
- fatalx("UTF-8 close invalid %#x", ictx->ch);
+ if (utf8_append(ud, ictx->ch) != UTF8_DONE) {
+ /*
+ * An error here could be invalid UTF-8 or it could be a
+ * nonprintable character for which we can't get the
+ * width. Drop it.
+ */
+ return (0);
+ }
log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
(int)ud->size, ud->data, ud->width);
diff --git a/usr.bin/tmux/key-string.c b/usr.bin/tmux/key-string.c
index 3b3988bbf05..d930e4e446a 100644
--- a/usr.bin/tmux/key-string.c
+++ b/usr.bin/tmux/key-string.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: key-string.c,v 1.34 2016/01/19 15:59:12 nicm Exp $ */
+/* $OpenBSD: key-string.c,v 1.35 2016/03/02 15:36:02 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -149,6 +149,7 @@ key_string_lookup_string(const char *string)
struct utf8_data ud;
u_int i;
enum utf8_state more;
+ wchar_t wc;
/* Is this no key? */
if (strcasecmp(string, "None") == 0)
@@ -185,8 +186,9 @@ key_string_lookup_string(const char *string)
more = utf8_append(&ud, (u_char)string[i]);
if (more != UTF8_DONE)
return (KEYC_UNKNOWN);
- key = utf8_combine(&ud);
- return (key | modifiers);
+ if (utf8_combine(&ud, &wc) != UTF8_DONE)
+ return (KEYC_UNKNOWN);
+ return (wc | modifiers);
}
/* Otherwise look the key up in the table. */
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index dfa1d6d921b..b0cf163db40 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.621 2016/03/01 12:02:08 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.622 2016/03/02 15:36:02 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -2316,8 +2316,7 @@ void utf8_set(struct utf8_data *, u_char);
void utf8_copy(struct utf8_data *, const struct utf8_data *);
enum utf8_state utf8_open(struct utf8_data *, u_char);
enum utf8_state utf8_append(struct utf8_data *, u_char);
-u_int utf8_width(wchar_t);
-wchar_t utf8_combine(const struct utf8_data *);
+enum utf8_state utf8_combine(const struct utf8_data *, wchar_t *);
enum utf8_state utf8_split(wchar_t, struct utf8_data *);
int utf8_strvis(char *, const char *, size_t, int);
char *utf8_sanitize(const char *);
diff --git a/usr.bin/tmux/tty-keys.c b/usr.bin/tmux/tty-keys.c
index 3ee25dbb68e..dcfb8ea6d5d 100644
--- a/usr.bin/tmux/tty-keys.c
+++ b/usr.bin/tmux/tty-keys.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tty-keys.c,v 1.84 2016/01/19 15:59:12 nicm Exp $ */
+/* $OpenBSD: tty-keys.c,v 1.85 2016/03/02 15:36:03 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -477,6 +477,7 @@ tty_keys_next(struct tty *tty)
struct utf8_data ud;
enum utf8_state more;
u_int i;
+ wchar_t wc;
/* Get key buffer. */
buf = EVBUFFER_DATA(tty->event->input);
@@ -552,7 +553,11 @@ first_key:
more = utf8_append(&ud, (u_char)buf[i]);
if (more != UTF8_DONE)
goto discard_key;
- key = utf8_combine(&ud);
+
+ if (utf8_combine(&ud, &wc) != UTF8_DONE)
+ goto discard_key;
+ key = wc;
+
log_debug("UTF-8 key %.*s %#llx", (int)size, buf, key);
goto complete_key;
}
diff --git a/usr.bin/tmux/utf8.c b/usr.bin/tmux/utf8.c
index e9571e7a22f..9c9aa223cb2 100644
--- a/usr.bin/tmux/utf8.c
+++ b/usr.bin/tmux/utf8.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: utf8.c,v 1.28 2016/03/01 12:02:08 nicm Exp $ */
+/* $OpenBSD: utf8.c,v 1.29 2016/03/02 15:36:03 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -25,6 +25,8 @@
#include "tmux.h"
+static int utf8_width(wchar_t);
+
/* Set a single character. */
void
utf8_set(struct utf8_data *ud, u_char ch)
@@ -80,6 +82,9 @@ utf8_open(struct utf8_data *ud, u_char ch)
enum utf8_state
utf8_append(struct utf8_data *ud, u_char ch)
{
+ wchar_t wc;
+ int width;
+
if (ud->have >= ud->size)
fatalx("UTF-8 character overflow");
if (ud->size > sizeof ud->data)
@@ -94,39 +99,49 @@ utf8_append(struct utf8_data *ud, u_char ch)
if (ud->width == 0xff)
return (UTF8_ERROR);
- ud->width = utf8_width(utf8_combine(ud));
+
+ if (utf8_combine(ud, &wc) != UTF8_DONE)
+ return (UTF8_ERROR);
+ if ((width = utf8_width(wc)) < 0)
+ return (UTF8_ERROR);
+ ud->width = width;
+
return (UTF8_DONE);
}
/* Get width of Unicode character. */
-u_int
+static int
utf8_width(wchar_t wc)
{
- int width;
+ int width;
width = wcwidth(wc);
- if (width < 0)
- return (0);
+ if (width < 0 || width > 0xff)
+ return (-1);
return (width);
}
/* Combine UTF-8 into Unicode. */
-wchar_t
-utf8_combine(const struct utf8_data *ud)
+enum utf8_state
+utf8_combine(const struct utf8_data *ud, wchar_t *wc)
{
- wchar_t wc;
-
- if (mbtowc(&wc, ud->data, ud->size) <= 0)
- return (0xfffd);
- return (wc);
+ switch (mbtowc(wc, ud->data, ud->size)) {
+ case -1:
+ mbtowc(NULL, NULL, MB_CUR_MAX);
+ return (UTF8_ERROR);
+ case 0:
+ return (UTF8_ERROR);
+ default:
+ return (UTF8_DONE);
+ }
}
/* Split Unicode into UTF-8. */
enum utf8_state
utf8_split(wchar_t wc, struct utf8_data *ud)
{
- char s[MB_CUR_MAX];
- int slen;
+ char s[MB_LEN_MAX];
+ int slen;
slen = wctomb(s, wc);
if (slen <= 0 || slen > (int)sizeof ud->data)