diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | common/pango.c | 34 |
2 files changed, 23 insertions, 12 deletions
@@ -5,6 +5,7 @@ install_manifest.txt bin/ test/ build/ +build-*/ .lvimrc config-debug wayland-*-protocol.* diff --git a/common/pango.c b/common/pango.c index db8413f7..7e164043 100644 --- a/common/pango.c +++ b/common/pango.c @@ -10,10 +10,6 @@ #include "log.h" #include "stringop.h" -#define MAX_CHARS 16384 - -static const char overflow[] = "[buffer overflow]"; - size_t escape_markup_text(const char *src, char *dest) { size_t length = 0; if (dest) { @@ -88,13 +84,19 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, int *baseline, double scale, bool markup, const char *fmt, ...) { - char buf[MAX_CHARS]; - va_list args; va_start(args, fmt); - if (vsnprintf(buf, sizeof(buf), fmt, args) >= MAX_CHARS) { - strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); + // Add one since vsnprintf excludes null terminator. + int length = vsnprintf(NULL, 0, fmt, args) + 1; + va_end(args); + + char *buf = malloc(length); + if (buf == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate memory"); + return; } + va_start(args, fmt); + vsnprintf(buf, length, fmt, args); va_end(args); PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); @@ -104,17 +106,24 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; } g_object_unref(layout); + free(buf); } void pango_printf(cairo_t *cairo, const char *font, double scale, bool markup, const char *fmt, ...) { - char buf[MAX_CHARS]; - va_list args; va_start(args, fmt); - if (vsnprintf(buf, sizeof(buf), fmt, args) >= MAX_CHARS) { - strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); + // Add one since vsnprintf excludes null terminator. + int length = vsnprintf(NULL, 0, fmt, args) + 1; + va_end(args); + + char *buf = malloc(length); + if (buf == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate memory"); + return; } + va_start(args, fmt); + vsnprintf(buf, length, fmt, args); va_end(args); PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); @@ -125,4 +134,5 @@ void pango_printf(cairo_t *cairo, const char *font, pango_cairo_update_layout(cairo, layout); pango_cairo_show_layout(cairo, layout); g_object_unref(layout); + free(buf); } |