From 291bae1e4426e698e29e31e819ebb6b2b5f02e23 Mon Sep 17 00:00:00 2001 From: Connor E <38229097+c-edw@users.noreply.github.com> Date: Fri, 18 Jan 2019 12:27:42 +0000 Subject: [PATCH 1/2] Remove VLAs. --- main.c | 3 ++- meson.build | 1 + pango.c | 12 ++++++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 5c21817..0b97f85 100644 --- a/main.c +++ b/main.c @@ -936,10 +936,11 @@ static int load_config(char *path, struct swaylock_state *state, } swaylock_log(LOG_DEBUG, "Config Line #%d: %s", line_number, line); - char flag[nread + 3]; + char *flag = malloc(nread + 3); sprintf(flag, "--%s", line); char *argv[] = {"swaylock", flag}; result = parse_options(2, argv, state, line_mode, NULL); + free(flag); if (result != 0) { break; } diff --git a/meson.build b/meson.build index 85e5fdd..32965e2 100644 --- a/meson.build +++ b/meson.build @@ -14,6 +14,7 @@ add_project_arguments( '-Wno-unused-parameter', '-Wno-unused-result', '-Wundef', + '-Wvla', ], language: 'c', ) diff --git a/pango.c b/pango.c index 097816c..f40750b 100644 --- a/pango.c +++ b/pango.c @@ -93,11 +93,11 @@ 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]; + char *buf = malloc(max_chars); va_list args; va_start(args, fmt); - if (vsnprintf(buf, sizeof(buf), fmt, args) >= max_chars) { + if (vsnprintf(buf, max_chars, fmt, args) >= max_chars) { strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); } va_end(args); @@ -109,15 +109,17 @@ 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]; + char *buf = malloc(max_chars); va_list args; va_start(args, fmt); - if (vsnprintf(buf, sizeof(buf), fmt, args) >= max_chars) { + if (vsnprintf(buf, max_chars, fmt, args) >= max_chars) { strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); } va_end(args); @@ -130,4 +132,6 @@ 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); } From 7c0ba8919712a1e23eca4b4785ab9f30f9e49a7e Mon Sep 17 00:00:00 2001 From: Connor E <38229097+c-edw@users.noreply.github.com> Date: Fri, 18 Jan 2019 13:45:34 +0000 Subject: [PATCH 2/2] Handle alloc failures. --- main.c | 6 ++++++ pango.c | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/main.c b/main.c index 0b97f85..000b16a 100644 --- a/main.c +++ b/main.c @@ -937,6 +937,12 @@ static int load_config(char *path, struct swaylock_state *state, swaylock_log(LOG_DEBUG, "Config Line #%d: %s", line_number, line); char *flag = malloc(nread + 3); + if (flag == NULL) { + free(line); + free(config); + swaylock_log(LOG_ERROR, "Failed to allocate memory"); + return 0; + } sprintf(flag, "--%s", line); char *argv[] = {"swaylock", flag}; result = parse_options(2, argv, state, line_mode, NULL); diff --git a/pango.c b/pango.c index f40750b..8e80015 100644 --- a/pango.c +++ b/pango.c @@ -94,6 +94,10 @@ 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 = malloc(max_chars); + if (buf == NULL) { + swaylock_log(LOG_ERROR, "Failed to allocate memory"); + return; + } va_list args; va_start(args, fmt); @@ -116,6 +120,10 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, void pango_printf(cairo_t *cairo, const char *font, double scale, bool markup, const char *fmt, ...) { char *buf = malloc(max_chars); + if (buf == NULL) { + swaylock_log(LOG_ERROR, "Failed to allocate memory"); + return; + } va_list args; va_start(args, fmt);