mirror of
https://github.com/swaywm/sway.git
synced 2024-11-22 07:51:28 +00:00
Allocate minimum size necessary in pango text functions. (#3473)
* Allocate minimum size necessary in pango text functions. * Handle malloc failure.
This commit is contained in:
parent
a3d3504072
commit
9df3a9136c
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,6 +5,7 @@ install_manifest.txt
|
||||||
bin/
|
bin/
|
||||||
test/
|
test/
|
||||||
build/
|
build/
|
||||||
|
build-*/
|
||||||
.lvimrc
|
.lvimrc
|
||||||
config-debug
|
config-debug
|
||||||
wayland-*-protocol.*
|
wayland-*-protocol.*
|
||||||
|
|
|
@ -10,10 +10,6 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "stringop.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 escape_markup_text(const char *src, char *dest) {
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
if (dest) {
|
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,
|
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
|
||||||
int *baseline, double scale, bool markup, const char *fmt, ...) {
|
int *baseline, double scale, bool markup, const char *fmt, ...) {
|
||||||
char buf[MAX_CHARS];
|
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
if (vsnprintf(buf, sizeof(buf), fmt, args) >= MAX_CHARS) {
|
// Add one since vsnprintf excludes null terminator.
|
||||||
strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow);
|
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);
|
va_end(args);
|
||||||
|
|
||||||
PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
|
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;
|
*baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
|
||||||
}
|
}
|
||||||
g_object_unref(layout);
|
g_object_unref(layout);
|
||||||
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pango_printf(cairo_t *cairo, const char *font,
|
void pango_printf(cairo_t *cairo, const char *font,
|
||||||
double scale, bool markup, const char *fmt, ...) {
|
double scale, bool markup, const char *fmt, ...) {
|
||||||
char buf[MAX_CHARS];
|
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
if (vsnprintf(buf, sizeof(buf), fmt, args) >= MAX_CHARS) {
|
// Add one since vsnprintf excludes null terminator.
|
||||||
strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow);
|
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);
|
va_end(args);
|
||||||
|
|
||||||
PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
|
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_update_layout(cairo, layout);
|
||||||
pango_cairo_show_layout(cairo, layout);
|
pango_cairo_show_layout(cairo, layout);
|
||||||
g_object_unref(layout);
|
g_object_unref(layout);
|
||||||
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue