Skip to content

Commit 8149bc3

Browse files
author
nicm
committed
Be more strict about escape sequences that rename windows or set titles:
ignore any that not valid UTF-8 outright, and for good measure pass the result through our UTF-8-aware vis(3).
1 parent adf5628 commit 8149bc3

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

input.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,10 @@ input_exit_osc(struct input_ctx *ictx)
18961896
switch (option) {
18971897
case 0:
18981898
case 2:
1899-
screen_set_title(ictx->ctx.s, p);
1900-
server_status_window(ictx->wp->window);
1899+
if (utf8_isvalid(p)) {
1900+
screen_set_title(ictx->ctx.s, p);
1901+
server_status_window(ictx->wp->window);
1902+
}
19011903
break;
19021904
case 4:
19031905
input_osc_4(ictx->wp, p);
@@ -1909,7 +1911,7 @@ input_exit_osc(struct input_ctx *ictx)
19091911
input_osc_11(ictx->wp, p);
19101912
break;
19111913
case 12:
1912-
if (*p != '?') /* ? is colour request */
1914+
if (utf8_isvalid(p) && *p != '?') /* ? is colour request */
19131915
screen_set_cursor_colour(ictx->ctx.s, p);
19141916
break;
19151917
case 52:
@@ -1945,6 +1947,8 @@ input_exit_apc(struct input_ctx *ictx)
19451947
return;
19461948
log_debug("%s: \"%s\"", __func__, ictx->input_buf);
19471949

1950+
if (!utf8_isvalid(ictx->input_buf))
1951+
return;
19481952
screen_set_title(ictx->ctx.s, ictx->input_buf);
19491953
server_status_window(ictx->wp->window);
19501954
}
@@ -1968,9 +1972,10 @@ input_exit_rename(struct input_ctx *ictx)
19681972
return;
19691973
log_debug("%s: \"%s\"", __func__, ictx->input_buf);
19701974

1975+
if (!utf8_isvalid(ictx->input_buf))
1976+
return;
19711977
window_set_name(ictx->wp->window, ictx->input_buf);
19721978
options_set_number(ictx->wp->window->options, "automatic-rename", 0);
1973-
19741979
server_status_window(ictx->wp->window);
19751980
}
19761981

screen.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stdlib.h>
2222
#include <string.h>
2323
#include <unistd.h>
24+
#include <vis.h>
2425

2526
#include "tmux.h"
2627

@@ -107,7 +108,7 @@ void
107108
screen_set_title(struct screen *s, const char *title)
108109
{
109110
free(s->title);
110-
s->title = xstrdup(title);
111+
utf8_stravis(&s->title, title, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
111112
}
112113

113114
/* Resize screen. */

tmux.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,7 @@ enum utf8_state utf8_open(struct utf8_data *, u_char);
23182318
enum utf8_state utf8_append(struct utf8_data *, u_char);
23192319
enum utf8_state utf8_combine(const struct utf8_data *, wchar_t *);
23202320
enum utf8_state utf8_split(wchar_t, struct utf8_data *);
2321+
int utf8_isvalid(const char *);
23212322
int utf8_strvis(char *, const char *, size_t, int);
23222323
int utf8_stravis(char **, const char *, int);
23232324
char *utf8_sanitize(const char *);

utf8.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,31 @@ utf8_stravis(char **dst, const char *src, int flag)
207207
return (len);
208208
}
209209

210+
/* Does this string contain anything that isn't valid UTF-8? */
211+
int
212+
utf8_isvalid(const char *s)
213+
{
214+
struct utf8_data ud;
215+
const char *end;
216+
enum utf8_state more;
217+
size_t i;
218+
219+
end = s + strlen(s);
220+
while (s < end) {
221+
if ((more = utf8_open(&ud, *s)) == UTF8_MORE) {
222+
while (++s < end && more == UTF8_MORE)
223+
more = utf8_append(&ud, *s);
224+
if (more == UTF8_DONE)
225+
continue;
226+
return (0);
227+
}
228+
if (*s < 0x20 || *s > 0x7e)
229+
return (0);
230+
s++;
231+
}
232+
return (1);
233+
}
234+
210235
/*
211236
* Sanitize a string, changing any UTF-8 characters to '_'. Caller should free
212237
* the returned string. Anything not valid printable ASCII or UTF-8 is

window.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <time.h>
3030
#include <unistd.h>
3131
#include <util.h>
32+
#include <vis.h>
3233

3334
#include "tmux.h"
3435

@@ -408,7 +409,7 @@ void
408409
window_set_name(struct window *w, const char *new_name)
409410
{
410411
free(w->name);
411-
w->name = xstrdup(new_name);
412+
utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
412413
notify_window("window-renamed", w);
413414
}
414415

0 commit comments

Comments
 (0)