From 2d343755ce193ad2b489feee2aa80f2f2563c203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 29 Nov 2019 23:32:27 +0100 Subject: [PATCH] particle/string: mbstowcs() may fail. When it does, don't crash --- particles/string.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/particles/string.c b/particles/string.c index b857f49..f7b9085 100644 --- a/particles/string.c +++ b/particles/string.c @@ -45,19 +45,23 @@ begin_expose(struct exposable *exposable) font->name, font->fextents.ascent, font->fextents.descent, font->fextents.height); - size_t chars = mbstowcs(NULL, e->text, 0); - wchar_t wtext[chars + 1]; - mbstowcs(wtext, e->text, chars + 1); - - e->glyphs = malloc(chars * sizeof(e->glyphs[0])); + e->glyphs = NULL; e->num_glyphs = 0; - /* Convert text to glyph masks/images. */ - for (size_t i = 0; i < chars; i++) { - const struct glyph *glyph = font_glyph_for_wc(font, wtext[i]); - if (glyph == NULL) - continue; - e->glyphs[e->num_glyphs++] = glyph; + size_t chars = mbstowcs(NULL, e->text, 0); + if (chars != (size_t)-1) { + wchar_t wtext[chars + 1]; + mbstowcs(wtext, e->text, chars + 1); + + e->glyphs = malloc(chars * sizeof(e->glyphs[0])); + + /* Convert text to glyph masks/images. */ + for (size_t i = 0; i < chars; i++) { + const struct glyph *glyph = font_glyph_for_wc(font, wtext[i]); + if (glyph == NULL) + continue; + e->glyphs[e->num_glyphs++] = glyph; + } } exposable->width = exposable->particle->left_margin +