Handle calloc failures

This commit is contained in:
Drew DeVault 2016-12-15 18:26:53 -05:00
parent a2b9149656
commit 10c8b73075
8 changed files with 39 additions and 2 deletions

View file

@ -23,11 +23,14 @@ struct cmd_results *cmd_assign(int argc, char **argv) {
char *movecmd = "move container to workspace "; char *movecmd = "move container to workspace ";
int arglen = strlen(movecmd) + strlen(*argv) + 1; int arglen = strlen(movecmd) + strlen(*argv) + 1;
char *cmdlist = calloc(1, arglen); 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); snprintf(cmdlist, arglen, "%s%s", movecmd, *argv);
struct criteria *crit = malloc(sizeof(struct criteria)); struct criteria *crit = malloc(sizeof(struct criteria));
if (!crit) { if (!crit) {
free(cmdlist);
return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria"); return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria");
} }
crit->crit_raw = strdup(criteria); crit->crit_raw = strdup(criteria);

View file

@ -26,6 +26,9 @@ struct cmd_results *cmd_output(int argc, char **argv) {
const char *name = argv[0]; const char *name = argv[0];
struct output_config *output = calloc(1, sizeof(struct output_config)); 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->x = output->y = output->width = output->height = -1;
output->name = strdup(name); output->name = strdup(name);
output->enabled = -1; output->enabled = -1;

View file

@ -61,6 +61,10 @@ struct cmd_results *cmd_workspace(int argc, char **argv) {
return error; return error;
} }
struct workspace_output *wso = calloc(1, sizeof(struct workspace_output)); 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->workspace = strdup(argv[0]);
wso->output = strdup(argv[2]); wso->output = strdup(argv[2]);
int i = -1; int i = -1;

View file

@ -494,6 +494,9 @@ bool load_main_config(const char *file, bool is_active) {
struct sway_config *old_config = config; struct sway_config *old_config = config;
config = calloc(1, sizeof(struct sway_config)); config = calloc(1, sizeof(struct sway_config));
if (!config) {
sway_abort("Unable to allocate config");
}
config_defaults(config); config_defaults(config);
if (is_active) { if (is_active) {

View file

@ -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 // next id starts at 1 because 0 is assigned to root_container in layout.c
static size_t next_id = 1; static size_t next_id = 1;
swayc_t *c = calloc(1, sizeof(swayc_t)); swayc_t *c = calloc(1, sizeof(swayc_t));
if (!c) {
return NULL;
}
c->id = next_id++; c->id = next_id++;
c->handle = -1; c->handle = -1;
c->gaps = -1; c->gaps = -1;

View file

@ -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); sway_log(L_DEBUG, "Creating panel config for resource %p", resource);
struct panel_config *config = calloc(1, sizeof(struct panel_config)); 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); list_add(desktop_shell.panels, config);
config->wl_resource = resource; config->wl_resource = resource;
return config; return config;

View file

@ -11,8 +11,16 @@
struct input_config *new_input_config(const char* identifier) { struct input_config *new_input_config(const char* identifier) {
struct input_config *input = calloc(1, sizeof(struct input_config)); 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); 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->tap = INT_MIN;
input->drag_lock = INT_MIN; input->drag_lock = INT_MIN;

View file

@ -245,6 +245,15 @@ json_object *ipc_json_get_version() {
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE #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); 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, SWAY_GIT_VERSION);
strcat(full_version, " ("); strcat(full_version, " (");
strcat(full_version, SWAY_VERSION_DATE); strcat(full_version, SWAY_VERSION_DATE);