Fix pango escaping and refactor escape_markup_text

Fixes #2674.

The cause of the issue was in get_pango_layout. When we call
pango_parse_markup, `text` is the escaped string, and the unescaped
string is then computed and written to `buf`. We were then passing the
unescaped string to pango_layout_set_markup, but this function needs the
escaped string. `buf` is not needed and has been removed.

The other part of this PR refactors escape_markup_text to remove the
dest_length argument and removes the -1 return value on error. It now
assumes that you've allocated dest to the correct length.
master
Ryan Dwyer 6 years ago
parent fe7e66407c
commit 10ef118e09

@ -7,66 +7,45 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "log.h" #include "log.h"
#include "stringop.h"
int escape_markup_text(const char *src, char *dest, int dest_length) { size_t escape_markup_text(const char *src, char *dest) {
int length = 0; size_t length = 0;
if (dest) {
dest[0] = '\0';
}
while (src[0]) { while (src[0]) {
switch (src[0]) { switch (src[0]) {
case '&': case '&':
length += 5; length += 5;
if (dest && dest_length - length >= 0) { lenient_strcat(dest, "&amp;");
dest += sprintf(dest, "%s", "&amp;");
} else {
dest_length = -1;
}
break; break;
case '<': case '<':
length += 4; length += 4;
if (dest && dest_length - length >= 0) { lenient_strcat(dest, "&lt;");
dest += sprintf(dest, "%s", "&lt;");
} else {
dest_length = -1;
}
break; break;
case '>': case '>':
length += 4; length += 4;
if (dest && dest_length - length >= 0) { lenient_strcat(dest, "&gt;");
dest += sprintf(dest, "%s", "&gt;");
} else {
dest_length = -1;
}
break; break;
case '\'': case '\'':
length += 6; length += 6;
if (dest && dest_length - length >= 0) { lenient_strcat(dest, "&apos;");
dest += sprintf(dest, "%s", "&apos;");
} else {
dest_length = -1;
}
break; break;
case '"': case '"':
length += 6; length += 6;
if (dest && dest_length - length >= 0) { lenient_strcat(dest, "&quot;");
dest += sprintf(dest, "%s", "&quot;");
} else {
dest_length = -1;
}
break; break;
default: default:
length += 1; if (dest) {
if (dest && dest_length - length >= 0) { dest[length] = *src;
*(dest++) = *src; dest[length + 1] = '\0';
} else {
dest_length = -1;
} }
length += 1;
} }
src++; src++;
} }
// if we could not fit the escaped string in dest, return -1
if (dest && dest_length == -1) {
return -1;
}
return length; return length;
} }
@ -75,11 +54,9 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
PangoLayout *layout = pango_cairo_create_layout(cairo); PangoLayout *layout = pango_cairo_create_layout(cairo);
PangoAttrList *attrs; PangoAttrList *attrs;
if (markup) { if (markup) {
char *buf;
GError *error = NULL; GError *error = NULL;
if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) { if (pango_parse_markup(text, -1, 0, &attrs, NULL, NULL, &error)) {
pango_layout_set_markup(layout, buf, -1); pango_layout_set_markup(layout, text, -1);
free(buf);
} else { } else {
wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text, wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text,
error->message); error->message);

@ -6,17 +6,13 @@
#include <cairo/cairo.h> #include <cairo/cairo.h>
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
/* Utility function which escape characters a & < > ' ". /**
* Utility function which escape characters a & < > ' ".
* *
* If the dest parameter is NULL, then the function returns the length of * The function returns the length of the escaped string, optionally writing the
* of the escaped src string. The dest_length doesn't matter. * escaped string to dest if provided.
*
* If the dest parameter is not NULL then the fuction escapes the src string
* an puts the escaped string in dest and returns the lenght of the escaped string.
* The dest_length parameter is the size of dest array. If the size of dest is not
* enough, then the function returns -1.
*/ */
int escape_markup_text(const char *src, char *dest, int dest_length); size_t escape_markup_text(const char *src, char *dest);
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
const char *text, double scale, bool markup); const char *text, double scale, bool markup);
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,

@ -785,14 +785,9 @@ static size_t parse_title_format(struct sway_view *view, char *buffer) {
} }
static char *escape_title(char *buffer) { static char *escape_title(char *buffer) {
int length = escape_markup_text(buffer, NULL, 0); size_t length = escape_markup_text(buffer, NULL);
char *escaped_title = calloc(length + 1, sizeof(char)); char *escaped_title = calloc(length + 1, sizeof(char));
int result = escape_markup_text(buffer, escaped_title, length); escape_markup_text(buffer, escaped_title);
if (result != length) {
wlr_log(WLR_ERROR, "Could not escape title: %s", buffer);
free(escaped_title);
return buffer;
}
free(buffer); free(buffer);
return escaped_title; return escaped_title;
} }

Loading…
Cancel
Save