From 7a77958ba20c2b344be5e66e69e2f212a4bbf6b3 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 8 Aug 2020 20:34:30 +0100 Subject: Convert most dynamic allocations to use functions from xmalloc.h --- wayland.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index 4dca5ef5..2b5ab9b8 100644 --- a/wayland.c +++ b/wayland.c @@ -29,6 +29,7 @@ #include "render.h" #include "selection.h" #include "util.h" +#include "xmalloc.h" static void csd_instantiate(struct wl_window *win) @@ -250,7 +251,7 @@ seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) { struct seat *seat = data; free(seat->name); - seat->name = strdup(name); + seat->name = xstrdup(name); } static const struct wl_seat_listener seat_listener = { @@ -318,8 +319,8 @@ output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, mon->dim.mm.width = physical_width; mon->dim.mm.height = physical_height; mon->inch = sqrt(pow(mon->dim.mm.width, 2) + pow(mon->dim.mm.height, 2)) * 0.03937008; - mon->make = make != NULL ? strdup(make) : NULL; - mon->model = model != NULL ? strdup(model) : NULL; + mon->make = make != NULL ? xstrdup(make) : NULL; + mon->model = model != NULL ? xstrdup(model) : NULL; mon->subpixel = subpixel; output_update_ppi(mon); } @@ -389,7 +390,7 @@ xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output, const char *name) { struct monitor *mon = data; - mon->name = name != NULL ? strdup(name) : NULL; + mon->name = name != NULL ? xstrdup(name) : NULL; } static void @@ -397,7 +398,7 @@ xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output, const char *description) { struct monitor *mon = data; - mon->description = description != NULL ? strdup(description) : NULL; + mon->description = description != NULL ? xstrdup(description) : NULL; } static const struct zxdg_output_v1_listener xdg_output_listener = { @@ -973,7 +974,7 @@ fdm_wayl(struct fdm *fdm, int fd, int events, void *data) struct wayland * wayl_init(const struct config *conf, struct fdm *fdm) { - struct wayland *wayl = calloc(1, sizeof(*wayl)); + struct wayland *wayl = xcalloc(1, sizeof(*wayl)); wayl->conf = conf; wayl->fdm = fdm; wayl->fd = -1; @@ -1130,7 +1131,7 @@ wayl_win_init(struct terminal *term) struct wayland *wayl = term->wl; const struct config *conf = term->conf; - struct wl_window *win = calloc(1, sizeof(*win)); + struct wl_window *win = xcalloc(1, sizeof(*win)); win->term = term; win->use_csd = CSD_UNKNOWN; win->csd.move_timeout_fd = -1; -- cgit v1.3 From 4e6100494c4fdea43cb95df1abb68958a89d7935 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sun, 9 Aug 2020 08:55:20 +0100 Subject: Handle allocation failures explicitly in *_init() functions --- reaper.c | 10 ++++++++-- wayland.c | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'wayland.c') diff --git a/reaper.c b/reaper.c index e74dd6cd..adc89e3c 100644 --- a/reaper.c +++ b/reaper.c @@ -10,7 +10,6 @@ #define LOG_ENABLE_DBG 0 #include "log.h" #include "tllist.h" -#include "xmalloc.h" struct reaper { struct fdm *fdm; @@ -40,7 +39,14 @@ reaper_init(struct fdm *fdm) return NULL; } - struct reaper *reaper = xmalloc(sizeof(*reaper)); + struct reaper *reaper = malloc(sizeof(*reaper)); + if (unlikely(reaper == NULL)) { + LOG_ERRNO("malloc() failed"); + close(fd); + sigprocmask(SIG_UNBLOCK, &mask, NULL); + return NULL; + } + *reaper = (struct reaper){ .fdm = fdm, .fd = fd, diff --git a/wayland.c b/wayland.c index 2b5ab9b8..5f12d531 100644 --- a/wayland.c +++ b/wayland.c @@ -974,7 +974,12 @@ fdm_wayl(struct fdm *fdm, int fd, int events, void *data) struct wayland * wayl_init(const struct config *conf, struct fdm *fdm) { - struct wayland *wayl = xcalloc(1, sizeof(*wayl)); + struct wayland *wayl = calloc(1, sizeof(*wayl)); + if (unlikely(wayl == NULL)) { + LOG_ERRNO("calloc() failed"); + return NULL; + } + wayl->conf = conf; wayl->fdm = fdm; wayl->fd = -1; @@ -1131,7 +1136,12 @@ wayl_win_init(struct terminal *term) struct wayland *wayl = term->wl; const struct config *conf = term->conf; - struct wl_window *win = xcalloc(1, sizeof(*win)); + struct wl_window *win = calloc(1, sizeof(*win)); + if (unlikely(win == NULL)) { + LOG_ERRNO("calloc() failed"); + return NULL; + } + win->term = term; win->use_csd = CSD_UNKNOWN; win->csd.move_timeout_fd = -1; -- cgit v1.3