From 10c8b73075fa0dd5512cc14be7240ec47f68dece Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 15 Dec 2016 18:26:53 -0500 Subject: [PATCH] Handle calloc failures --- sway/commands/assign.c | 5 ++++- sway/commands/output.c | 3 +++ sway/commands/workspace.c | 4 ++++ sway/config.c | 3 +++ sway/container.c | 3 +++ sway/extensions.c | 4 ++++ sway/input.c | 10 +++++++++- sway/ipc-json.c | 9 +++++++++ 8 files changed, 39 insertions(+), 2 deletions(-) diff --git a/sway/commands/assign.c b/sway/commands/assign.c index 1824692b3..992b46923 100644 --- a/sway/commands/assign.c +++ b/sway/commands/assign.c @@ -23,11 +23,14 @@ struct cmd_results *cmd_assign(int argc, char **argv) { char *movecmd = "move container to workspace "; int arglen = strlen(movecmd) + strlen(*argv) + 1; char *cmdlist = calloc(1, arglen); - + if (!cmdlist) { + return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate command list"); + } snprintf(cmdlist, arglen, "%s%s", movecmd, *argv); struct criteria *crit = malloc(sizeof(struct criteria)); if (!crit) { + free(cmdlist); return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria"); } crit->crit_raw = strdup(criteria); diff --git a/sway/commands/output.c b/sway/commands/output.c index e150aed2b..01ac9f4e8 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -26,6 +26,9 @@ struct cmd_results *cmd_output(int argc, char **argv) { const char *name = argv[0]; struct output_config *output = calloc(1, sizeof(struct output_config)); + if (!output) { + return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config"); + } output->x = output->y = output->width = output->height = -1; output->name = strdup(name); output->enabled = -1; diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c index 35224f8a4..14fe242fe 100644 --- a/sway/commands/workspace.c +++ b/sway/commands/workspace.c @@ -61,6 +61,10 @@ struct cmd_results *cmd_workspace(int argc, char **argv) { return error; } struct workspace_output *wso = calloc(1, sizeof(struct workspace_output)); + if (!wso) { + return cmd_results_new(CMD_FAILURE, "workspace output", + "Unable to allocate workspace output"); + } wso->workspace = strdup(argv[0]); wso->output = strdup(argv[2]); int i = -1; diff --git a/sway/config.c b/sway/config.c index b86bacdb4..4164cefa9 100644 --- a/sway/config.c +++ b/sway/config.c @@ -494,6 +494,9 @@ bool load_main_config(const char *file, bool is_active) { struct sway_config *old_config = config; config = calloc(1, sizeof(struct sway_config)); + if (!config) { + sway_abort("Unable to allocate config"); + } config_defaults(config); if (is_active) { diff --git a/sway/container.c b/sway/container.c index 8a584efa3..d9677cdb1 100644 --- a/sway/container.c +++ b/sway/container.c @@ -23,6 +23,9 @@ static swayc_t *new_swayc(enum swayc_types type) { // next id starts at 1 because 0 is assigned to root_container in layout.c static size_t next_id = 1; swayc_t *c = calloc(1, sizeof(swayc_t)); + if (!c) { + return NULL; + } c->id = next_id++; c->handle = -1; c->gaps = -1; diff --git a/sway/extensions.c b/sway/extensions.c index 759cbb84b..40702e28d 100644 --- a/sway/extensions.c +++ b/sway/extensions.c @@ -23,6 +23,10 @@ static struct panel_config *find_or_create_panel_config(struct wl_resource *reso } sway_log(L_DEBUG, "Creating panel config for resource %p", resource); struct panel_config *config = calloc(1, sizeof(struct panel_config)); + if (!config) { + sway_log(L_ERROR, "Unable to create panel config"); + return NULL; + } list_add(desktop_shell.panels, config); config->wl_resource = resource; return config; diff --git a/sway/input.c b/sway/input.c index 61757ab85..249d95c65 100644 --- a/sway/input.c +++ b/sway/input.c @@ -11,8 +11,16 @@ struct input_config *new_input_config(const char* identifier) { struct input_config *input = calloc(1, sizeof(struct input_config)); + if (!input) { + sway_log(L_DEBUG, "Unable to allocate input config"); + return NULL; + } sway_log(L_DEBUG, "new_input_config(%s)", identifier); - input->identifier = strdup(identifier); + if (!(input->identifier = strdup(identifier))) { + free(input); + sway_log(L_DEBUG, "Unable to allocate input config"); + return NULL; + } input->tap = INT_MIN; input->drag_lock = INT_MIN; diff --git a/sway/ipc-json.c b/sway/ipc-json.c index e65e9de35..fd17216e0 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -245,6 +245,15 @@ json_object *ipc_json_get_version() { #if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE char *full_version = calloc(strlen(SWAY_GIT_VERSION) + strlen(SWAY_GIT_BRANCH) + strlen(SWAY_VERSION_DATE) + 20, 1); + if (!full_version) { + json_object_object_add(version, "human_readable", + json_object_new_string("Allocating version string failed")); + // TODO: it's stupid that we allocate this in the first place + json_object_object_add(version, "major", json_object_new_int(0)); + json_object_object_add(version, "minor", json_object_new_int(0)); + json_object_object_add(version, "patch", json_object_new_int(0)); + return version; + } strcat(full_version, SWAY_GIT_VERSION); strcat(full_version, " ("); strcat(full_version, SWAY_VERSION_DATE);