From 789a877b379cd35c350610be62b971ae00feb542 Mon Sep 17 00:00:00 2001 From: Heghedus Razvan Date: Mon, 7 May 2018 19:30:45 +0300 Subject: Fix crash when using pango markup font The characters & < > ' " needs to be escaped when using pango markup Signed-off-by: Heghedus Razvan --- common/pango.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 5 deletions(-) (limited to 'common/pango.c') diff --git a/common/pango.c b/common/pango.c index 658d2876..9437c60d 100644 --- a/common/pango.c +++ b/common/pango.c @@ -8,6 +8,68 @@ #include #include "log.h" +int escape_markup_text(const char *src, char *dest, int dest_length) { + int length = 0; + + while (src[0]) { + switch (src[0]) { + case '&': + length += 5; + if (dest && dest_length - length >= 0) { + dest += sprintf(dest, "%s", "&"); + } else { + dest_length = -1; + } + break; + case '<': + length += 4; + if (dest && dest_length - length >= 0) { + dest += sprintf(dest, "%s", "<"); + } else { + dest_length = -1; + } + break; + case '>': + length += 4; + if (dest && dest_length - length >= 0) { + dest += sprintf(dest, "%s", ">"); + } else { + dest_length = -1; + } + break; + case '\'': + length += 6; + if (dest && dest_length - length >= 0) { + dest += sprintf(dest, "%s", "'"); + } else { + dest_length = -1; + } + break; + case '"': + length += 6; + if (dest && dest_length - length >= 0) { + dest += sprintf(dest, "%s", """); + } else { + dest_length = -1; + } + break; + default: + length += 1; + if (dest && dest_length - length >= 0) { + *(dest++) = *src; + } else { + dest_length = -1; + } + } + src++; + } + // if we could not fit the escaped string in dest, return -1 + if (dest && dest_length == -1) { + return -1; + } + return length; +} + PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text, int32_t scale, bool markup) { PangoLayout *layout = pango_cairo_create_layout(cairo); @@ -15,13 +77,14 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, if (markup) { char *buf; GError *error = NULL; - if (!sway_assert(pango_parse_markup( - text, -1, 0, &attrs, &buf, NULL, &error), - "pango_parse_markup '%s' -> error %s", text, - error ? error->message : NULL)) { + bool result = pango_parse_markup(text, -1, 0, &attrs, &buf, + NULL, &error); + if (result) { + wlr_log(L_ERROR, "pango_parse_markup '%s' -> error %s", text, + error->message); return NULL; } - pango_layout_set_markup(layout, buf, -1); + pango_layout_set_markup(layout, text, -1); free(buf); } else { attrs = pango_attr_list_new(); -- cgit v1.2.3-54-g00ecf