aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--base64.c12
-rw-r--r--client.c3
-rw-r--r--config.c2
-rw-r--r--csi.c3
-rw-r--r--extract.c10
-rw-r--r--fdm.c10
-rw-r--r--grid.c9
-rw-r--r--input.c5
-rw-r--r--main.c11
-rw-r--r--meson.build5
-rw-r--r--osc.c19
-rw-r--r--reaper.c3
-rw-r--r--render.c12
-rw-r--r--selection.c11
-rw-r--r--server.c18
-rw-r--r--shm.c3
-rw-r--r--sixel.c17
-rw-r--r--slave.c9
-rw-r--r--terminal.c21
-rw-r--r--vt.c3
-rw-r--r--wayland.c15
21 files changed, 133 insertions, 68 deletions
diff --git a/base64.c b/base64.c
index 214d6943..3acfbe34 100644
--- a/base64.c
+++ b/base64.c
@@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
+#include <errno.h>
#define LOG_MODULE "base64"
#define LOG_ENABLE_DBG 0
@@ -47,10 +48,14 @@ base64_decode(const char *s)
{
const size_t len = strlen(s);
- if (len % 4 != 0)
+ if (len % 4 != 0) {
+ errno = EINVAL;
return NULL;
+ }
char *ret = malloc(len / 4 * 3 + 1);
+ if (unlikely(ret == NULL))
+ return NULL;
for (size_t i = 0, o = 0; i < len; i += 4, o += 3) {
unsigned char c0 = s[i + 0];
@@ -60,6 +65,7 @@ base64_decode(const char *s)
if (!is_valid(c0) || !is_valid(c1) || !is_valid(c2) || !is_valid(c3)) {
free(ret);
+ errno = EINVAL;
return NULL;
}
@@ -87,10 +93,12 @@ char *
base64_encode(const uint8_t *data, size_t size)
{
assert(size % 3 == 0);
- if (size %3 != 0)
+ if (unlikely(size % 3 != 0))
return NULL;
char *ret = malloc(size / 3 * 4 + 1);
+ if (unlikely(ret == NULL))
+ return NULL;
for (size_t i = 0, o = 0; i < size; i += 3, o += 4) {
int x = data[i + 0];
diff --git a/client.c b/client.c
index 69a91833..f8a07daa 100644
--- a/client.c
+++ b/client.c
@@ -16,6 +16,7 @@
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "version.h"
+#include "xmalloc.h"
static volatile sig_atomic_t aborted = 0;
@@ -201,7 +202,7 @@ main(int argc, char *const *argv)
errno = 0;
size_t buf_len = 1024;
do {
- cwd = realloc(cwd, buf_len);
+ cwd = xrealloc(cwd, buf_len);
if (getcwd(cwd, buf_len) == NULL && errno != ERANGE) {
LOG_ERRNO("failed to get current working directory");
goto err;
diff --git a/config.c b/config.c
index 64caa4ab..aeea91b5 100644
--- a/config.c
+++ b/config.c
@@ -752,7 +752,7 @@ parse_section_key_bindings(
}
pipe_len = pipe_cmd_end - value - 1;
- pipe_cmd = strndup(&value[1], pipe_len);
+ pipe_cmd = xstrndup(&value[1], pipe_len);
if (!tokenize_cmdline(pipe_cmd, &pipe_argv)) {
LOG_AND_NOTIFY_ERR("%s:%d: syntax error in command line", path, lineno);
diff --git a/csi.c b/csi.c
index a0a3eba4..541be13e 100644
--- a/csi.c
+++ b/csi.c
@@ -18,6 +18,7 @@
#include "util.h"
#include "version.h"
#include "vt.h"
+#include "xmalloc.h"
#define UNHANDLED() LOG_DBG("unhandled: %s", csi_as_string(term, final, -1))
#define UNHANDLED_SGR(idx) LOG_DBG("unhandled: %s", csi_as_string(term, 'm', idx))
@@ -933,7 +934,7 @@ csi_dispatch(struct terminal *term, uint8_t final)
unsigned what = vt_param_get(term, 1, 0);
if (what == 0 || what == 2) {
tll_push_back(
- term->window_title_stack, strdup(term->window_title));
+ term->window_title_stack, xstrdup(term->window_title));
}
break;
}
diff --git a/extract.c b/extract.c
index 45dbee3f..49a37f14 100644
--- a/extract.c
+++ b/extract.c
@@ -20,6 +20,11 @@ struct extraction_context *
extract_begin(enum selection_kind kind)
{
struct extraction_context *ctx = malloc(sizeof(*ctx));
+ if (unlikely(ctx == NULL)) {
+ LOG_ERRNO("malloc() failed");
+ return NULL;
+ }
+
*ctx = (struct extraction_context){
.selection_kind = kind,
};
@@ -80,6 +85,11 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len)
}
*text = malloc(_len + 1);
+ if (unlikely(text == NULL)) {
+ LOG_ERRNO("malloc() failed");
+ goto out;
+ }
+
wcstombs(*text, ctx->buf, _len + 1);
if (len != NULL)
diff --git a/fdm.c b/fdm.c
index e6f955cc..93ac86ff 100644
--- a/fdm.c
+++ b/fdm.c
@@ -50,6 +50,11 @@ fdm_init(void)
}
struct fdm *fdm = malloc(sizeof(*fdm));
+ if (unlikely(fdm == NULL)) {
+ LOG_ERRNO("malloc() failed");
+ return NULL;
+ }
+
*fdm = (struct fdm){
.epoll_fd = epoll_fd,
.is_polling = false,
@@ -114,6 +119,11 @@ fdm_add(struct fdm *fdm, int fd, int events, fdm_handler_t handler, void *data)
#endif
struct handler *fd_data = malloc(sizeof(*fd_data));
+ if (unlikely(fd_data == NULL)) {
+ LOG_ERRNO("malloc() failed");
+ return false;
+ }
+
*fd_data = (struct handler) {
.fd = fd,
.events = events,
diff --git a/grid.c b/grid.c
index 4486393b..24a67903 100644
--- a/grid.c
+++ b/grid.c
@@ -8,6 +8,7 @@
#include "log.h"
#include "sixel.h"
#include "util.h"
+#include "xmalloc.h"
void
grid_swap_row(struct grid *grid, int row_a, int row_b)
@@ -28,16 +29,16 @@ grid_swap_row(struct grid *grid, int row_a, int row_b)
struct row *
grid_row_alloc(int cols, bool initialize)
{
- struct row *row = malloc(sizeof(*row));
+ struct row *row = xmalloc(sizeof(*row));
row->dirty = false;
row->linebreak = false;
if (initialize) {
- row->cells = calloc(cols, sizeof(row->cells[0]));
+ row->cells = xcalloc(cols, sizeof(row->cells[0]));
for (size_t c = 0; c < cols; c++)
row->cells[c].attrs.clean = 1;
} else
- row->cells = malloc(cols * sizeof(row->cells[0]));
+ row->cells = xmalloc(cols * sizeof(row->cells[0]));
return row;
}
@@ -65,7 +66,7 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols,
int new_col_idx = 0;
int new_row_idx = 0;
- struct row **new_grid = calloc(new_rows, sizeof(new_grid[0]));
+ struct row **new_grid = xcalloc(new_rows, sizeof(new_grid[0]));
struct row *new_row = new_grid[new_row_idx];
assert(new_row == NULL);
diff --git a/input.c b/input.c
index feaa6072..55090ff7 100644
--- a/input.c
+++ b/input.c
@@ -36,6 +36,7 @@
#include "tokenize.h"
#include "util.h"
#include "vt.h"
+#include "xmalloc.h"
struct pipe_context {
char *text;
@@ -227,7 +228,7 @@ execute_binding(struct seat *seat, struct terminal *term,
/* Close read end */
close(pipe_fd[0]);
- ctx = malloc(sizeof(*ctx));
+ ctx = xmalloc(sizeof(*ctx));
*ctx = (struct pipe_context){
.text = text,
.left = len,
@@ -268,7 +269,7 @@ input_parse_key_binding(struct xkb_keymap *keymap, const char *combos,
xkb_keysym_t sym = XKB_KEY_NoSymbol;
- char *copy = strdup(combos);
+ char *copy = xstrdup(combos);
for (char *save1 = NULL, *combo = strtok_r(copy, " ", &save1);
combo != NULL;
diff --git a/main.c b/main.c
index 9c3bfe6f..b40685be 100644
--- a/main.c
+++ b/main.c
@@ -28,6 +28,7 @@
#include "shm.h"
#include "terminal.h"
#include "version.h"
+#include "xmalloc.h"
static volatile sig_atomic_t aborted = 0;
@@ -341,15 +342,15 @@ main(int argc, char *const *argv)
if (conf_term != NULL) {
free(conf.term);
- conf.term = strdup(conf_term);
+ conf.term = xstrdup(conf_term);
}
if (conf_title != NULL) {
free(conf.title);
- conf.title = strdup(conf_title);
+ conf.title = xstrdup(conf_title);
}
if (conf_app_id != NULL) {
free(conf.app_id);
- conf.app_id = strdup(conf_app_id);
+ conf.app_id = xstrdup(conf_app_id);
}
if (login_shell)
conf.login_shell = true;
@@ -367,7 +368,7 @@ main(int argc, char *const *argv)
conf.height = conf_height;
if (conf_server_socket_path != NULL) {
free(conf.server_socket_path);
- conf.server_socket_path = strdup(conf_server_socket_path);
+ conf.server_socket_path = xstrdup(conf_server_socket_path);
}
if (maximized)
conf.startup_mode = STARTUP_MAXIMIZED;
@@ -389,7 +390,7 @@ main(int argc, char *const *argv)
errno = 0;
size_t buf_len = 1024;
do {
- cwd = realloc(cwd, buf_len);
+ cwd = xrealloc(cwd, buf_len);
if (getcwd(cwd, buf_len) == NULL && errno != ERANGE) {
LOG_ERRNO("failed to get current working directory");
goto out;
diff --git a/meson.build b/meson.build
index 19d91156..e5863667 100644
--- a/meson.build
+++ b/meson.build
@@ -139,7 +139,10 @@ executable(
executable(
'footclient',
'client.c',
- 'log.c', 'log.h', version,
+ 'log.c', 'log.h',
+ 'macros.h',
+ 'xmalloc.c', 'xmalloc.h',
+ version,
install: true)
tic = find_program('tic', native: true)
diff --git a/osc.c b/osc.c
index 39a9cb6f..b79dcdc4 100644
--- a/osc.c
+++ b/osc.c
@@ -2,6 +2,7 @@
#include <string.h>
#include <ctype.h>
+#include <errno.h>
#include <limits.h>
#include <unistd.h>
@@ -14,6 +15,7 @@
#include "selection.h"
#include "terminal.h"
#include "vt.h"
+#include "xmalloc.h"
#define UNHANDLED() LOG_DBG("unhandled: OSC: %.*s", (int)term->vt.osc.idx, term->vt.osc.data)
@@ -22,13 +24,16 @@ osc_to_clipboard(struct terminal *term, const char *target,
const char *base64_data)
{
char *decoded = base64_decode(base64_data);
- LOG_DBG("decoded: %s", decoded);
-
if (decoded == NULL) {
- LOG_WARN("OSC: invalid clipboard data: %s", base64_data);
+ if (errno == EINVAL)
+ LOG_WARN("OSC: invalid clipboard data: %s", base64_data);
+ else
+ LOG_ERRNO("base64_decode() failed");
return;
}
+ LOG_DBG("decoded: %s", decoded);
+
bool to_clipboard = false;
bool to_primary = false;
@@ -67,13 +72,13 @@ osc_to_clipboard(struct terminal *term, const char *target,
}
if (to_clipboard) {
- char *copy = strdup(decoded);
+ char *copy = xstrdup(decoded);
if (!text_to_clipboard(seat, term, copy, seat->kbd.serial))
free(copy);
}
if (to_primary) {
- char *copy = strdup(decoded);
+ char *copy = xstrdup(decoded);
if (!text_to_primary(seat, term, copy, seat->kbd.serial))
free(copy);
}
@@ -184,7 +189,7 @@ osc_from_clipboard(struct terminal *term, const char *source)
term_to_slave(term, &src, 1);
term_to_slave(term, ";", 1);
- struct clip_context *ctx = malloc(sizeof(*ctx));
+ struct clip_context *ctx = xmalloc(sizeof(*ctx));
*ctx = (struct clip_context) {.seat = seat, .term = term};
switch (src) {
@@ -374,7 +379,7 @@ osc_set_pwd(struct terminal *term, char *string)
/* Decode %xx encoded characters */
const char *path = hostname_end;
- char *pwd = malloc(strlen(path) + 1);
+ char *pwd = xmalloc(strlen(path) + 1);
char *p = pwd;
while (true) {
diff --git a/reaper.c b/reaper.c
index 83db9f47..e74dd6cd 100644
--- a/reaper.c
+++ b/reaper.c
@@ -10,6 +10,7 @@
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "tllist.h"
+#include "xmalloc.h"
struct reaper {
struct fdm *fdm;
@@ -39,7 +40,7 @@ reaper_init(struct fdm *fdm)
return NULL;
}
- struct reaper *reaper = malloc(sizeof(*reaper));
+ struct reaper *reaper = xmalloc(sizeof(*reaper));
*reaper = (struct reaper){
.fdm = fdm,
.fd = fd,
diff --git a/render.c b/render.c
index e0e9f3a5..de2c8002 100644
--- a/render.c
+++ b/render.c
@@ -22,6 +22,7 @@
#include "selection.h"
#include "shm.h"
#include "util.h"
+#include "xmalloc.h"
#define TIME_FRAME_RENDERING 0
#define TIME_SCROLL_DAMAGE 0
@@ -43,7 +44,12 @@ static void fdm_hook_refresh_pending_terminals(struct fdm *fdm, void *data);
struct renderer *
render_init(struct fdm *fdm, struct wayland *wayl)
{
- struct renderer *renderer = calloc(1, sizeof(*renderer));
+ struct renderer *renderer = malloc(sizeof(*renderer));
+ if (unlikely(renderer == NULL)) {
+ LOG_ERRNO("malloc() failed");
+ return NULL;
+ }
+
*renderer = (struct renderer) {
.fdm = fdm,
.wayl = wayl,
@@ -1691,7 +1697,7 @@ grid_render(struct terminal *term)
if (feedback == NULL) {
LOG_WARN("failed to create presentation feedback");
} else {
- struct presentation_context *ctx = malloc(sizeof(*ctx));
+ struct presentation_context *ctx = xmalloc(sizeof(*ctx));
*ctx = (struct presentation_context){
.term = term,
.input.tv_sec = term->render.input_time.tv_sec,
@@ -1840,7 +1846,7 @@ render_update_title(struct terminal *term)
char *copy = NULL;
if (strlen(title) > max_len) {
- copy = strndup(title, max_len);
+ copy = xstrndup(title, max_len);
title = copy;
}
diff --git a/selection.c b/selection.c
index 8dd93028..356e675d 100644
--- a/selection.c
+++ b/selection.c
@@ -20,6 +20,7 @@
#include "render.h"
#include "util.h"
#include "vt.h"
+#include "xmalloc.h"
bool
selection_enabled(const struct terminal *term, struct seat *seat)
@@ -692,9 +693,9 @@ send(void *data, struct wl_data_source *wl_data_source, const char *mime_type,
size_t async_idx = 0;
switch (async_write(fd, selection, len, &async_idx)) {
case ASYNC_WRITE_REMAIN: {
- struct clipboard_send *ctx = malloc(sizeof(*ctx));
+ struct clipboard_send *ctx = xmalloc(sizeof(*ctx));
*ctx = (struct clipboard_send) {
- .data = strdup(selection),
+ .data = xstrdup(selection),
.len = len,
.idx = async_idx,
};
@@ -784,9 +785,9 @@ primary_send(void *data,
size_t async_idx = 0;
switch (async_write(fd, selection, len, &async_idx)) {
case ASYNC_WRITE_REMAIN: {
- struct clipboard_send *ctx = malloc(sizeof(*ctx));
+ struct clipboard_send *ctx = xmalloc(sizeof(*ctx));
*ctx = (struct clipboard_send) {
- .data = strdup(selection),
+ .data = xstrdup(selection),
.len = len,
.idx = async_idx,
};
@@ -953,7 +954,7 @@ begin_receive_clipboard(struct terminal *term, int read_fd,
return done(user);
}
- struct clipboard_receive *ctx = malloc(sizeof(*ctx));
+ struct clipboard_receive *ctx = xmalloc(sizeof(*ctx));
*ctx = (struct clipboard_receive) {
.cb = cb,
.done = done,
diff --git a/server.c b/server.c
index c2778ad7..75b52b65 100644
--- a/server.c
+++ b/server.c
@@ -19,6 +19,7 @@
#include "shm.h"
#include "terminal.h"
#include "wayland.h"
+#include "xmalloc.h"
struct client;
struct server {
@@ -157,7 +158,7 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
}
LOG_DBG("total len: %u", total_len);
- client->buffer.data = malloc(total_len + 1);
+ client->buffer.data = xmalloc(total_len + 1);
client->buffer.left = total_len;
client->buffer.idx = 0;
@@ -277,7 +278,7 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
memcpy(&argc, p, sizeof(argc));
p += sizeof(argc);
- argv = calloc(argc + 1, sizeof(argv[0]));
+ argv = xcalloc(argc + 1, sizeof(argv[0]));
LOG_DBG("argc = %d", argc);
for (int i = 0; i < argc; i++) {
@@ -302,11 +303,11 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
client->conf = *server->conf;
client->conf.term = strlen(term_env) > 0
- ? strdup(term_env) : strdup(server->conf->term);
+ ? xstrdup(term_env) : xstrdup(server->conf->term);
client->conf.title = strlen(title) > 0
- ? strdup(title) : strdup(server->conf->title);
+ ? xstrdup(title) : xstrdup(server->conf->title);
client->conf.app_id = strlen(app_id) > 0
- ? strdup(app_id) : strdup(server->conf->app_id);
+ ? xstrdup(app_id) : xstrdup(server->conf->app_id);
client->conf.hold_at_exit = hold;
client->conf.login_shell = login_shell;
@@ -360,7 +361,7 @@ fdm_server(struct fdm *fdm, int fd, int events, void *data)
return false;
}
- struct client *client = malloc(sizeof(*client));
+ struct client *client = xmalloc(sizeof(*client));
*client = (struct client) {
.server = server,
.fd = client_fd,
@@ -451,6 +452,11 @@ server_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper,
}
server = malloc(sizeof(*server));
+ if (unlikely(server == NULL)) {
+ LOG_ERRNO("malloc() failed");
+ goto err;
+ }
+
*server = (struct server) {
.conf = conf,
.fdm = fdm,
diff --git a/shm.c b/shm.c
index 317bc9c9..e4d3378e 100644
--- a/shm.c
+++ b/shm.c
@@ -23,6 +23,7 @@
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "macros.h"
+#include "xmalloc.h"
#define TIME_SCROLL 0
@@ -150,7 +151,7 @@ instantiate_offset(struct wl_shm *shm, struct buffer *buf, off_t new_offset)
void *mmapped = MAP_FAILED;
struct wl_buffer *wl_buf = NULL;
- pixman_image_t **pix = calloc(buf->pix_instances, sizeof(*pix));
+ pixman_image_t **pix = xcalloc(buf->pix_instances, sizeof(*pix));
mmapped = (uint8_t *)buf->real_mmapped + new_offset;
diff --git a/sixel.c b/sixel.c
index ab9ad0e9..13596088 100644
--- a/sixel.c
+++ b/sixel.c
@@ -9,6 +9,7 @@
#include "render.h"
#include "sixel-hls.h"
#include "util.h"
+#include "xmalloc.h"
static size_t count;
@@ -38,13 +39,13 @@ sixel_init(struct terminal *term)
term->sixel.param = 0;
term->sixel.param_idx = 0;
memset(term->sixel.params, 0, sizeof(term->sixel.params));
- term->sixel.image.data = malloc(1 * 6 * sizeof(term->sixel.image.data[0]));
+ term->sixel.image.data = xmalloc(1 * 6 * sizeof(term->sixel.image.data[0]));
term->sixel.image.width = 1;
term->sixel.image.height = 6;
term->sixel.image.autosize = true;
if (term->sixel.palette == NULL) {
- term->sixel.palette = calloc(
+ term->sixel.palette = xcalloc(
term->sixel.palette_size, sizeof(term->sixel.palette[0]));
}
@@ -240,7 +241,7 @@ sixel_overwrite(struct terminal *term, struct sixel *six,
.cols = six->cols,
.pos = six->pos,
};
- imgs[0].data = malloc(imgs[0].width * imgs[0].height * sizeof(uint32_t));
+ imgs[0].data = xmalloc(imgs[0].width * imgs[0].height * sizeof(uint32_t));
memcpy(imgs[0].data, six->data, imgs[0].width * imgs[0].height * sizeof(uint32_t));
}
@@ -254,7 +255,7 @@ sixel_overwrite(struct terminal *term, struct sixel *six,
six->pos.col,
(six->pos.row + rel_below) & (term->grid->num_rows - 1)},
};
- imgs[1].data = malloc(imgs[1].width * imgs[1].height * sizeof(uint32_t));
+ imgs[1].data = xmalloc(imgs[1].width * imgs[1].height * sizeof(uint32_t));
memcpy(
imgs[1].data,
&((const uint32_t *)six->data)[rel_below * term->cell_height * six->width],
@@ -271,7 +272,7 @@ sixel_overwrite(struct terminal *term, struct sixel *six,
six->pos.col,
(six->pos.row + rel_above) & (term->grid->num_rows - 1)},
};
- imgs[2].data = malloc(imgs[2].width * imgs[2].height * sizeof(uint32_t));
+ imgs[2].data = xmalloc(imgs[2].width * imgs[2].height * sizeof(uint32_t));
for (size_t i = 0; i < imgs[2].height; i++)
memcpy(
&((uint32_t *)imgs[2].data)[i * imgs[2].width],
@@ -289,7 +290,7 @@ sixel_overwrite(struct terminal *term, struct sixel *six,
six->pos.col + rel_right,
(six->pos.row + rel_above) & (term->grid->num_rows - 1)},
};
- imgs[3].data = malloc(imgs[3].width * imgs[3].height * sizeof(uint32_t));
+ imgs[3].data = xmalloc(imgs[3].width * imgs[3].height * sizeof(uint32_t));
for (size_t i = 0; i < imgs[3].height; i++)
memcpy(
&((uint32_t *)imgs[3].data)[i * imgs[3].width],
@@ -464,7 +465,7 @@ sixel_unhook(struct terminal *term)
if (pixel_row_idx == 0)
img_data = term->sixel.image.data;
else {
- img_data = malloc(height * stride);
+ img_data = xmalloc(height * stride);
memcpy(
img_data,
&((uint8_t *)term->sixel.image.data)[pixel_row_idx * stride],
@@ -566,7 +567,7 @@ resize(struct terminal *term, int new_width, int new_height)
} else {
/* Width (and thus stride) change - need to allocate a new buffer */
assert(new_width > old_width);
- new_data = malloc(alloc_new_width * alloc_new_height * sizeof(uint32_t));
+ new_data = xmalloc(alloc_new_width * alloc_new_height * sizeof(uint32_t));
/* Copy old rows, and initialize new columns to background color */
for (int r = 0; r < old_height; r++) {
diff --git a/slave.c b/slave.c
index cb6a2f27..a6667507 100644
--- a/slave.c
+++ b/slave.c
@@ -20,6 +20,7 @@
#include "terminal.h"
#include "tokenize.h"
+#include "xmalloc.h"
static bool
is_valid_shell(const char *shell)
@@ -217,9 +218,9 @@ slave_exec(int ptmx, char *argv[], int err_fd, bool login_shell,
const char *file;
if (login_shell) {
- file = strdup(argv[0]);
+ file = xstrdup(argv[0]);
- char *arg0 = malloc(strlen(argv[0]) + 1 + 1);
+ char *arg0 = xmalloc(strlen(argv[0]) + 1 + 1);
arg0[0] = '-';
arg0[1] = '\0';
strcat(arg0, argv[0]);
@@ -291,7 +292,7 @@ slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv,
char **shell_argv = NULL;
if (argc == 0) {
- char *shell_copy = strdup(conf_shell);
+ char *shell_copy = xstrdup(conf_shell);
if (!tokenize_cmdline(shell_copy, &_shell_argv)) {
free(shell_copy);
(void)!write(fork_pipe[1], &errno, sizeof(errno));
@@ -302,7 +303,7 @@ slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv,
size_t count = 0;
for (; argv[count] != NULL; count++)
;
- shell_argv = malloc((count + 1) * sizeof(shell_argv[0]));
+ shell_argv = xmalloc((count + 1) * sizeof(shell_argv[0]));
for (size_t i = 0; i < count; i++)
shell_argv[i] = argv[i];
shell_argv[count] = NULL;
diff --git a/terminal.c b/terminal.c
index 900f8fae..1c97ee53 100644
--- a/terminal.c
+++ b/terminal.c
@@ -33,6 +33,7 @@
#include "spawn.h"
#include "util.h"
#include "vt.h"
+#include "xmalloc.h"
#define PTMX_TIMING 0
@@ -93,7 +94,7 @@ enqueue_data:
* handler take care of it
*/
{
- void *copy = malloc(len);
+ void *copy = xmalloc(len);
memcpy(copy, _data, len);
struct ptmx_buffer queued = {
@@ -510,11 +511,11 @@ initialize_render_workers(struct terminal *term)
goto err_sem_destroy;
}
- term->render.workers.threads = calloc(
+ term->render.workers.threads = xcalloc(
term->render.workers.count, sizeof(term->render.workers.threads[0]));
for (size_t i = 0; i < term->render.workers.count; i++) {
- struct render_worker_context *ctx = malloc(sizeof(*ctx));
+ struct render_worker_context *ctx = xmalloc(sizeof(*ctx));
*ctx = (struct render_worker_context) {
.term = term,
.my_id = 1 + i,
@@ -685,7 +686,7 @@ reload_fonts(struct terminal *term)
snprintf(size, sizeof(size), ":size=%.2f", term->font_sizes[i].pt_size);
size_t len = strlen(it->item.pattern) + strlen(size) + 1;
- names[i] = malloc(len);
+ names[i] = xmalloc(len);
strcpy(names[i], it->item.pattern);
strcat(names[i], size);
@@ -767,6 +768,10 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper,
int app_sync_updates_fd = -1;
struct terminal *term = malloc(sizeof(*term));
+ if (unlikely(term == NULL)) {
+ LOG_ERRNO("malloc() failed");
+ return NULL;
+ }
if ((ptmx = posix_openpt(O_RDWR | O_NOCTTY)) == -1) {
LOG_ERRNO("failed to open PTY");
@@ -836,7 +841,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper,
.quit = false,
.ptmx = ptmx,
.ptmx_buffer = tll_init(),
- .font_sizes = malloc(sizeof(term->font_sizes[0]) * tll_length(conf->fonts)),
+ .font_sizes = xmalloc(sizeof(term->font_sizes[0]) * tll_length(conf->fonts)),
.font_dpi = 0.,
.font_subpixel = (conf->colors.alpha == 0xffff /* Can't do subpixel rendering on transparent background */
? FCFT_SUBPIXEL_DEFAULT
@@ -929,8 +934,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper,
.hold_at_exit = conf->hold_at_exit,
.shutdown_cb = shutdown_cb,
.shutdown_data = shutdown_data,
- .foot_exe = strdup(foot_exe),
- .cwd = strdup(cwd),
+ .foot_exe = xstrdup(foot_exe),
+ .cwd = xstrdup(cwd),
};
{
@@ -2228,7 +2233,7 @@ void
term_set_window_title(struct terminal *term, const char *title)
{
free(term->window_title);
- term->window_title = strdup(title);
+ term->window_title = xstrdup(title);
render_refresh_title(term);
}
diff --git a/vt.c b/vt.c
index f25f26be..d114b87b 100644
--- a/vt.c
+++ b/vt.c
@@ -13,6 +13,7 @@
#include "grid.h"
#include "osc.h"
#include "util.h"
+#include "xmalloc.h"
#define UNHANDLED() LOG_DBG("unhandled: %s", esc_as_string(term, final))
@@ -649,7 +650,7 @@ action_utf8_print(struct terminal *term, wchar_t wc)
if (term->composed_count < CELL_COMB_CHARS_HI) {
term->composed_count++;
- term->composed = realloc(term->composed, term->composed_count * sizeof(term->composed[0]));
+ term->composed = xrealloc(term->composed, term->composed_count * sizeof(term->composed[0]));
term->composed[term->composed_count - 1] = new_cc;
term_print(term, CELL_COMB_CHARS_LO + term->composed_count - 1, base_width);
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;