From 51d4c8dd4f793114cbc520f2a8465981bd1d4eaa Mon Sep 17 00:00:00 2001 From: NRK Date: Sun, 3 Jul 2022 12:45:50 +0200 Subject: [PATCH] check for utf8_decode errors (#327) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit utf8_decode() may return an errors, in which case the returned codepoint might be garbage. the issue can be tested by adding the following to `image-info` which produces invalid utf8 sequences: base64 -d << EOF 9JCAgPSQgIH0kICC9JCAg/SQgIT0kICF9JCAhvSQgIf0kICI9JCAifSQgIr0kICL9JCAjPSQgI30 kICO9JCAj/SQgJD0kICR9JCAkvSQgJP0kICU9JCAlfSQgJb0kICX9JCAmPSQgJn0kICa9JCAm/SQ gJz0kICd9JCAnvSQgJ8= EOF on my system, this leads to the statusbar being filled with empty boxes. check for returned error and skip the iteration. Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/327 Reviewed-by: Berke Kocaoğlu --- window.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/window.c b/window.c index f472474..1b3b6e0 100644 --- a/window.c +++ b/window.c @@ -407,7 +407,7 @@ void win_clear(win_t *win) static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y, char *text, int len, int w) { - int err, tw = 0; + int err, tw = 0, warned = 0; char *t, *next; uint32_t rune; XftFont *f; @@ -415,7 +415,14 @@ static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, XGlyphInfo ext; for (t = text; t - text < len; t = next) { + err = 0; next = utf8_decode(t, &rune, &err); + if (err) { + if (!warned) + error(0, 0, "error decoding utf8 status-bar text"); + warned = 1; + continue; + } if (XftCharExists(win->env.dpy, font, rune)) { f = font; } else { /* fallback font */