aboutsummaryrefslogtreecommitdiffstats
path: root/common/pango.c
diff options
context:
space:
mode:
authorLibravatar Heghedus Razvan <heghedus.razvan@protonmail.com>2018-05-07 19:30:45 +0300
committerLibravatar Heghedus Razvan <heghedus.razvan@gmail.com>2018-05-13 17:53:45 +0300
commit789a877b379cd35c350610be62b971ae00feb542 (patch)
tree417eb755118e7dedae3badeeaa7d713f204994e2 /common/pango.c
parentMerge pull request #1824 from snaggen/idle (diff)
downloadsway-789a877b379cd35c350610be62b971ae00feb542.tar.gz
sway-789a877b379cd35c350610be62b971ae00feb542.tar.zst
sway-789a877b379cd35c350610be62b971ae00feb542.zip
Fix crash when using pango markup font
The characters & < > ' " needs to be escaped when using pango markup Signed-off-by: Heghedus Razvan <heghedus.razvan@gmail.com>
Diffstat (limited to 'common/pango.c')
-rw-r--r--common/pango.c73
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 @@
8#include <string.h> 8#include <string.h>
9#include "log.h" 9#include "log.h"
10 10
11int escape_markup_text(const char *src, char *dest, int dest_length) {
12 int length = 0;
13
14 while (src[0]) {
15 switch (src[0]) {
16 case '&':
17 length += 5;
18 if (dest && dest_length - length >= 0) {
19 dest += sprintf(dest, "%s", "&amp;");
20 } else {
21 dest_length = -1;
22 }
23 break;
24 case '<':
25 length += 4;
26 if (dest && dest_length - length >= 0) {
27 dest += sprintf(dest, "%s", "&lt;");
28 } else {
29 dest_length = -1;
30 }
31 break;
32 case '>':
33 length += 4;
34 if (dest && dest_length - length >= 0) {
35 dest += sprintf(dest, "%s", "&gt;");
36 } else {
37 dest_length = -1;
38 }
39 break;
40 case '\'':
41 length += 6;
42 if (dest && dest_length - length >= 0) {
43 dest += sprintf(dest, "%s", "&apos;");
44 } else {
45 dest_length = -1;
46 }
47 break;
48 case '"':
49 length += 6;
50 if (dest && dest_length - length >= 0) {
51 dest += sprintf(dest, "%s", "&quot;");
52 } else {
53 dest_length = -1;
54 }
55 break;
56 default:
57 length += 1;
58 if (dest && dest_length - length >= 0) {
59 *(dest++) = *src;
60 } else {
61 dest_length = -1;
62 }
63 }
64 src++;
65 }
66 // if we could not fit the escaped string in dest, return -1
67 if (dest && dest_length == -1) {
68 return -1;
69 }
70 return length;
71}
72
11PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, 73PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
12 const char *text, int32_t scale, bool markup) { 74 const char *text, int32_t scale, bool markup) {
13 PangoLayout *layout = pango_cairo_create_layout(cairo); 75 PangoLayout *layout = pango_cairo_create_layout(cairo);
@@ -15,13 +77,14 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
15 if (markup) { 77 if (markup) {
16 char *buf; 78 char *buf;
17 GError *error = NULL; 79 GError *error = NULL;
18 if (!sway_assert(pango_parse_markup( 80 bool result = pango_parse_markup(text, -1, 0, &attrs, &buf,
19 text, -1, 0, &attrs, &buf, NULL, &error), 81 NULL, &error);
20 "pango_parse_markup '%s' -> error %s", text, 82 if (result) {
21 error ? error->message : NULL)) { 83 wlr_log(L_ERROR, "pango_parse_markup '%s' -> error %s", text,
84 error->message);
22 return NULL; 85 return NULL;
23 } 86 }
24 pango_layout_set_markup(layout, buf, -1); 87 pango_layout_set_markup(layout, text, -1);
25 free(buf); 88 free(buf);
26 } else { 89 } else {
27 attrs = pango_attr_list_new(); 90 attrs = pango_attr_list_new();