nsxiv51d4c8d
check for utf8_decode errors (#327)
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.
+8 −1
1 file changed
- M window.c +8 −1
| @@ -407,7 +407,7 @@ void win_clear(win_t *win) | |||
| 407 | 407 | static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, | |
| 408 | 408 | int x, int y, char *text, int len, int w) | |
| 409 | 409 | { | |
| 410 | − | int err, tw = 0; | |
| 410 | + | int err, tw = 0, warned = 0; | |
| 411 | 411 | char *t, *next; | |
| 412 | 412 | uint32_t rune; | |
| 413 | 413 | XftFont *f; | |
| @@ -415,7 +415,14 @@ static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, | |||
| 415 | 415 | XGlyphInfo ext; | |
| 416 | 416 | ||
| 417 | 417 | for (t = text; t - text < len; t = next) { | |
| 418 | + | err = 0; | |
| 418 | 419 | next = utf8_decode(t, &rune, &err); | |
| 420 | + | if (err) { | |
| 421 | + | if (!warned) | |
| 422 | + | error(0, 0, "error decoding utf8 status-bar text"); | |
| 423 | + | warned = 1; | |
| 424 | + | continue; | |
| 425 | + | } | |
| 419 | 426 | if (XftCharExists(win->env.dpy, font, rune)) { | |
| 420 | 427 | f = font; | |
| 421 | 428 | } else { /* fallback font */ | |