diff options
author | emersion <contact@emersion.fr> | 2018-05-13 16:16:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-13 16:16:16 +0100 |
commit | 4dfb0529f865e9af327ea0740ae97f3d3e4da6f1 (patch) | |
tree | 417eb755118e7dedae3badeeaa7d713f204994e2 /common | |
parent | 61aa0937a75d23c0f2a00a8cea52c08bf69164dd (diff) | |
parent | 789a877b379cd35c350610be62b971ae00feb542 (diff) |
Merge pull request #1935 from heghe/heghe/fix-pango-markup-crash
Fix crash when using pango markup font
Diffstat (limited to 'common')
-rw-r--r-- | common/pango.c | 73 |
1 files changed, 68 insertions, 5 deletions
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 <string.h> #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(); |