Handle config-related allocation failures

master
Drew DeVault 8 years ago
parent 248df18c24
commit d75a747a3d

@ -5,6 +5,9 @@
list_t *create_list(void) { list_t *create_list(void) {
list_t *list = malloc(sizeof(list_t)); list_t *list = malloc(sizeof(list_t));
if (!list) {
return NULL;
}
list->capacity = 10; list->capacity = 10;
list->length = 0; list->length = 0;
list->items = malloc(sizeof(void*) * list->capacity); list->items = malloc(sizeof(void*) * list->capacity);

@ -29,21 +29,30 @@ struct sway_config *config = NULL;
static void terminate_swaybar(pid_t pid); static void terminate_swaybar(pid_t pid);
static void free_variable(struct sway_variable *var) { static void free_variable(struct sway_variable *var) {
if (!var) {
return;
}
free(var->name); free(var->name);
free(var->value); free(var->value);
free(var); free(var);
} }
static void free_binding(struct sway_binding *bind) { static void free_binding(struct sway_binding *bind) {
if (!bind) {
return;
}
free_flat_list(bind->keys); free_flat_list(bind->keys);
free(bind->command); free(bind->command);
free(bind); free(bind);
} }
static void free_mode(struct sway_mode *mode) { static void free_mode(struct sway_mode *mode) {
if (!mode) {
return;
}
free(mode->name); free(mode->name);
int i; int i;
for (i = 0; i < mode->bindings->length; ++i) { for (i = 0; mode->bindings && i < mode->bindings->length; ++i) {
free_binding(mode->bindings->items[i]); free_binding(mode->bindings->items[i]);
} }
list_free(mode->bindings); list_free(mode->bindings);
@ -51,13 +60,16 @@ static void free_mode(struct sway_mode *mode) {
} }
static void free_bar(struct bar_config *bar) { static void free_bar(struct bar_config *bar) {
if (!bar) {
return;
}
free(bar->mode); free(bar->mode);
free(bar->hidden_state); free(bar->hidden_state);
free(bar->status_command); free(bar->status_command);
free(bar->font); free(bar->font);
free(bar->separator_symbol); free(bar->separator_symbol);
int i; int i;
for (i = 0; i < bar->bindings->length; ++i) { for (i = 0; bar->bindings && i < bar->bindings->length; ++i) {
free_sway_mouse_binding(bar->bindings->items[i]); free_sway_mouse_binding(bar->bindings->items[i]);
} }
list_free(bar->bindings); list_free(bar->bindings);
@ -96,16 +108,25 @@ static void free_bar(struct bar_config *bar) {
} }
void free_input_config(struct input_config *ic) { void free_input_config(struct input_config *ic) {
if (!ic) {
return;
}
free(ic->identifier); free(ic->identifier);
free(ic); free(ic);
} }
void free_output_config(struct output_config *oc) { void free_output_config(struct output_config *oc) {
if (!oc) {
return;
}
free(oc->name); free(oc->name);
free(oc); free(oc);
} }
static void free_workspace_output(struct workspace_output *wo) { static void free_workspace_output(struct workspace_output *wo) {
if (!wo) {
return;
}
free(wo->output); free(wo->output);
free(wo->workspace); free(wo->workspace);
free(wo); free(wo);
@ -134,6 +155,10 @@ void pid_workspace_add(struct pid_workspace *pw) {
struct pid_workspace *list_pw = NULL; struct pid_workspace *list_pw = NULL;
struct timespec ts; struct timespec ts;
time_t *now = malloc(sizeof(time_t)); time_t *now = malloc(sizeof(time_t));
if (!now) {
sway_log(L_ERROR, "Allocating time for pid_workspace failed");
return;
}
pid_workspace_cleanup(); pid_workspace_cleanup();
@ -168,65 +193,74 @@ void free_pid_workspace(struct pid_workspace *pw) {
} }
void free_command_policy(struct command_policy *policy) { void free_command_policy(struct command_policy *policy) {
if (!policy) {
return;
}
free(policy->command); free(policy->command);
free(policy); free(policy);
} }
void free_feature_policy(struct feature_policy *policy) { void free_feature_policy(struct feature_policy *policy) {
if (!policy) {
return;
}
free(policy->program); free(policy->program);
free(policy); free(policy);
} }
void free_config(struct sway_config *config) { void free_config(struct sway_config *config) {
if (!config) {
return;
}
int i; int i;
for (i = 0; i < config->symbols->length; ++i) { for (i = 0; config->symbols && i < config->symbols->length; ++i) {
free_variable(config->symbols->items[i]); free_variable(config->symbols->items[i]);
} }
list_free(config->symbols); list_free(config->symbols);
for (i = 0; i < config->modes->length; ++i) { for (i = 0; config->modes && i < config->modes->length; ++i) {
free_mode(config->modes->items[i]); free_mode(config->modes->items[i]);
} }
list_free(config->modes); list_free(config->modes);
for (i = 0; i < config->bars->length; ++i) { for (i = 0; config->bars && i < config->bars->length; ++i) {
free_bar(config->bars->items[i]); free_bar(config->bars->items[i]);
} }
list_free(config->bars); list_free(config->bars);
free_flat_list(config->cmd_queue); free_flat_list(config->cmd_queue);
for (i = 0; i < config->workspace_outputs->length; ++i) { for (i = 0; config->workspace_outputs && i < config->workspace_outputs->length; ++i) {
free_workspace_output(config->workspace_outputs->items[i]); free_workspace_output(config->workspace_outputs->items[i]);
} }
list_free(config->workspace_outputs); list_free(config->workspace_outputs);
for (i = 0; i < config->pid_workspaces->length; ++i) { for (i = 0; config->pid_workspaces && i < config->pid_workspaces->length; ++i) {
free_pid_workspace(config->pid_workspaces->items[i]); free_pid_workspace(config->pid_workspaces->items[i]);
} }
list_free(config->pid_workspaces); list_free(config->pid_workspaces);
for (i = 0; i < config->criteria->length; ++i) { for (i = 0; config->criteria && i < config->criteria->length; ++i) {
free_criteria(config->criteria->items[i]); free_criteria(config->criteria->items[i]);
} }
list_free(config->criteria); list_free(config->criteria);
for (i = 0; i < config->input_configs->length; ++i) { for (i = 0; config->input_configs && i < config->input_configs->length; ++i) {
free_input_config(config->input_configs->items[i]); free_input_config(config->input_configs->items[i]);
} }
list_free(config->input_configs); list_free(config->input_configs);
for (i = 0; i < config->output_configs->length; ++i) { for (i = 0; config->output_configs && i < config->output_configs->length; ++i) {
free_output_config(config->output_configs->items[i]); free_output_config(config->output_configs->items[i]);
} }
list_free(config->output_configs); list_free(config->output_configs);
for (i = 0; i < config->command_policies->length; ++i) { for (i = 0; config->command_policies && i < config->command_policies->length; ++i) {
free_command_policy(config->command_policies->items[i]); free_command_policy(config->command_policies->items[i]);
} }
list_free(config->command_policies); list_free(config->command_policies);
for (i = 0; i < config->feature_policies->length; ++i) { for (i = 0; config->feature_policies && i < config->feature_policies->length; ++i) {
free_feature_policy(config->feature_policies->items[i]); free_feature_policy(config->feature_policies->items[i]);
} }
list_free(config->feature_policies); list_free(config->feature_policies);
@ -243,37 +277,37 @@ void free_config(struct sway_config *config) {
static bool file_exists(const char *path) { static bool file_exists(const char *path) {
return access(path, R_OK) != -1; return path && access(path, R_OK) != -1;
} }
static void config_defaults(struct sway_config *config) { static void config_defaults(struct sway_config *config) {
config->symbols = create_list(); if (!(config->symbols = create_list())) goto cleanup;
config->modes = create_list(); if (!(config->modes = create_list())) goto cleanup;
config->bars = create_list(); if (!(config->bars = create_list())) goto cleanup;
config->workspace_outputs = create_list(); if (!(config->workspace_outputs = create_list())) goto cleanup;
config->pid_workspaces = create_list(); if (!(config->pid_workspaces = create_list())) goto cleanup;
config->criteria = create_list(); if (!(config->criteria = create_list())) goto cleanup;
config->input_configs = create_list(); if (!(config->input_configs = create_list())) goto cleanup;
config->output_configs = create_list(); if (!(config->output_configs = create_list())) goto cleanup;
config->cmd_queue = create_list(); if (!(config->cmd_queue = create_list())) goto cleanup;
config->current_mode = malloc(sizeof(struct sway_mode)); if (!(config->current_mode = malloc(sizeof(struct sway_mode)))) goto cleanup;
config->current_mode->name = malloc(sizeof("default")); if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup;
strcpy(config->current_mode->name, "default"); strcpy(config->current_mode->name, "default");
config->current_mode->bindings = create_list(); if (!(config->current_mode->bindings = create_list())) goto cleanup;
list_add(config->modes, config->current_mode); list_add(config->modes, config->current_mode);
config->floating_mod = 0; config->floating_mod = 0;
config->dragging_key = M_LEFT_CLICK; config->dragging_key = M_LEFT_CLICK;
config->resizing_key = M_RIGHT_CLICK; config->resizing_key = M_RIGHT_CLICK;
config->floating_scroll_up_cmd = strdup(""); if (!(config->floating_scroll_up_cmd = strdup(""))) goto cleanup;
config->floating_scroll_down_cmd = strdup(""); if (!(config->floating_scroll_down_cmd = strdup(""))) goto cleanup;
config->floating_scroll_left_cmd = strdup(""); if (!(config->floating_scroll_left_cmd = strdup(""))) goto cleanup;
config->floating_scroll_right_cmd = strdup(""); if (!(config->floating_scroll_right_cmd = strdup(""))) goto cleanup;
config->default_layout = L_NONE; config->default_layout = L_NONE;
config->default_orientation = L_NONE; config->default_orientation = L_NONE;
config->font = strdup("monospace 10"); if (!(config->font = strdup("monospace 10"))) goto cleanup;
config->font_height = get_font_text_height(config->font); config->font_height = get_font_text_height(config->font);
// floating view // floating view
@ -297,9 +331,9 @@ static void config_defaults(struct sway_config *config) {
config->gaps_inner = 0; config->gaps_inner = 0;
config->gaps_outer = 0; config->gaps_outer = 0;
config->active_bar_modifiers = create_list(); if (!(config->active_bar_modifiers = create_list())) goto cleanup;
config->config_chain = create_list(); if (!(config->config_chain = create_list())) goto cleanup;
config->current_config = NULL; config->current_config = NULL;
// borders // borders
@ -343,9 +377,13 @@ static void config_defaults(struct sway_config *config) {
config->border_colors.background = 0xFFFFFFFF; config->border_colors.background = 0xFFFFFFFF;
// Security // Security
config->command_policies = create_list(); if (!(config->command_policies = create_list())) goto cleanup;
config->feature_policies = create_list(); if (!(config->feature_policies = create_list())) goto cleanup;
config->ipc_policy = UINT32_MAX; config->ipc_policy = UINT32_MAX;
return;
cleanup:
sway_abort("Unable to allocate config structures");
} }
static int compare_modifiers(const void *left, const void *right) { static int compare_modifiers(const void *left, const void *right) {
@ -386,11 +424,15 @@ static char *get_config_path(void) {
if (!getenv("XDG_CONFIG_HOME")) { if (!getenv("XDG_CONFIG_HOME")) {
char *home = getenv("HOME"); char *home = getenv("HOME");
char *config_home = malloc(strlen(home) + strlen("/.config") + 1); char *config_home = malloc(strlen(home) + strlen("/.config") + 1);
strcpy(config_home, home); if (!config_home) {
strcat(config_home, "/.config"); sway_log(L_ERROR, "Unable to allocate $HOME/.config");
setenv("XDG_CONFIG_HOME", config_home, 1); } else {
sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); strcpy(config_home, home);
free(config_home); strcat(config_home, "/.config");
setenv("XDG_CONFIG_HOME", config_home, 1);
sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home);
free(config_home);
}
} }
wordexp_t p; wordexp_t p;
@ -491,6 +533,10 @@ static bool load_include_config(const char *path, const char *parent_dir, struct
if (len >= 1 && path[0] != '/') { if (len >= 1 && path[0] != '/') {
len = len + strlen(parent_dir) + 2; len = len + strlen(parent_dir) + 2;
full_path = malloc(len * sizeof(char)); full_path = malloc(len * sizeof(char));
if (!full_path) {
sway_log(L_ERROR, "Unable to allocate full path to included config");
return false;
}
snprintf(full_path, len, "%s/%s", parent_dir, path); snprintf(full_path, len, "%s/%s", parent_dir, path);
} }
@ -819,6 +865,10 @@ static void invoke_swaybar(struct bar_config *bar) {
// run custom swaybar // run custom swaybar
int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5; int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5;
char *command = malloc(len * sizeof(char)); char *command = malloc(len * sizeof(char));
if (!command) {
sway_log(L_ERROR, "Unable to allocate swaybar command string");
return;
}
snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id); snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id);
char *const cmd[] = { char *const cmd[] = {
@ -1055,6 +1105,11 @@ char *do_var_replacement(char *str) {
if (strncmp(find, var->name, vnlen) == 0) { if (strncmp(find, var->name, vnlen) == 0) {
int vvlen = strlen(var->value); int vvlen = strlen(var->value);
char *newstr = malloc(strlen(str) - vnlen + vvlen + 1); char *newstr = malloc(strlen(str) - vnlen + vvlen + 1);
if (!newstr) {
sway_log(L_ERROR,
"Unable to allocate replacement during variable expansion");
break;
}
char *newptr = newstr; char *newptr = newstr;
int offset = find - str; int offset = find - str;
strncpy(newptr, str, offset); strncpy(newptr, str, offset);
@ -1188,6 +1243,9 @@ void free_sway_mouse_binding(struct sway_mouse_binding *binding) {
struct sway_binding *sway_binding_dup(struct sway_binding *sb) { struct sway_binding *sway_binding_dup(struct sway_binding *sb) {
struct sway_binding *new_sb = malloc(sizeof(struct sway_binding)); struct sway_binding *new_sb = malloc(sizeof(struct sway_binding));
if (!new_sb) {
return NULL;
}
new_sb->order = sb->order; new_sb->order = sb->order;
new_sb->modifiers = sb->modifiers; new_sb->modifiers = sb->modifiers;
@ -1197,6 +1255,10 @@ struct sway_binding *sway_binding_dup(struct sway_binding *sb) {
int i; int i;
for (i = 0; i < sb->keys->length; ++i) { for (i = 0; i < sb->keys->length; ++i) {
xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
if (!key) {
free_sway_binding(new_sb);
return NULL;
}
*key = *(xkb_keysym_t *)sb->keys->items[i]; *key = *(xkb_keysym_t *)sb->keys->items[i];
list_add(new_sb->keys, key); list_add(new_sb->keys, key);
} }
@ -1207,13 +1269,16 @@ struct sway_binding *sway_binding_dup(struct sway_binding *sb) {
struct bar_config *default_bar_config(void) { struct bar_config *default_bar_config(void) {
struct bar_config *bar = NULL; struct bar_config *bar = NULL;
bar = malloc(sizeof(struct bar_config)); bar = malloc(sizeof(struct bar_config));
bar->mode = strdup("dock"); if (!bar) {
bar->hidden_state = strdup("hide"); return NULL;
}
if (!(bar->mode = strdup("dock"))) goto cleanup;
if (!(bar->hidden_state = strdup("hide"))) goto cleanup;
bar->modifier = WLC_BIT_MOD_LOGO; bar->modifier = WLC_BIT_MOD_LOGO;
bar->outputs = NULL; bar->outputs = NULL;
bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
bar->bindings = create_list(); if (!(bar->bindings = create_list())) goto cleanup;
bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p' && sleep 1; done"); if (!(bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p' && sleep 1; done"))) goto cleanup;
bar->pango_markup = false; bar->pango_markup = false;
bar->swaybar_command = NULL; bar->swaybar_command = NULL;
bar->font = NULL; bar->font = NULL;
@ -1227,21 +1292,21 @@ struct bar_config *default_bar_config(void) {
bar->verbose = false; bar->verbose = false;
bar->pid = 0; bar->pid = 0;
// set default colors // set default colors
bar->colors.background = strndup("#000000ff", 9); if (!(bar->colors.background = strndup("#000000ff", 9))) goto cleanup;
bar->colors.statusline = strndup("#ffffffff", 9); if (!(bar->colors.statusline = strndup("#ffffffff", 9))) goto cleanup;
bar->colors.separator = strndup("#666666ff", 9); if (!(bar->colors.separator = strndup("#666666ff", 9))) goto cleanup;
bar->colors.focused_workspace_border = strndup("#4c7899ff", 9); if (!(bar->colors.focused_workspace_border = strndup("#4c7899ff", 9))) goto cleanup;
bar->colors.focused_workspace_bg = strndup("#285577ff", 9); if (!(bar->colors.focused_workspace_bg = strndup("#285577ff", 9))) goto cleanup;
bar->colors.focused_workspace_text = strndup("#ffffffff", 9); if (!(bar->colors.focused_workspace_text = strndup("#ffffffff", 9))) goto cleanup;
bar->colors.active_workspace_border = strndup("#333333ff", 9); if (!(bar->colors.active_workspace_border = strndup("#333333ff", 9))) goto cleanup;
bar->colors.active_workspace_bg = strndup("#5f676aff", 9); if (!(bar->colors.active_workspace_bg = strndup("#5f676aff", 9))) goto cleanup;
bar->colors.active_workspace_text = strndup("#ffffffff", 9); if (!(bar->colors.active_workspace_text = strndup("#ffffffff", 9))) goto cleanup;
bar->colors.inactive_workspace_border = strndup("#333333ff", 9); if (!(bar->colors.inactive_workspace_border = strndup("#333333ff", 9))) goto cleanup;
bar->colors.inactive_workspace_bg = strndup("#222222ff", 9); if (!(bar->colors.inactive_workspace_bg = strndup("#222222ff", 9))) goto cleanup;
bar->colors.inactive_workspace_text = strndup("#888888ff", 9); if (!(bar->colors.inactive_workspace_text = strndup("#888888ff", 9))) goto cleanup;
bar->colors.urgent_workspace_border = strndup("#2f343aff", 9); if (!(bar->colors.urgent_workspace_border = strndup("#2f343aff", 9))) goto cleanup;
bar->colors.urgent_workspace_bg = strndup("#900000ff", 9); if (!(bar->colors.urgent_workspace_bg = strndup("#900000ff", 9))) goto cleanup;
bar->colors.urgent_workspace_text = strndup("#ffffffff", 9); if (!(bar->colors.urgent_workspace_text = strndup("#ffffffff", 9))) goto cleanup;
// if the following colors stay undefined, they fall back to background, // if the following colors stay undefined, they fall back to background,
// statusline, separator and urgent_workspace_*. // statusline, separator and urgent_workspace_*.
bar->colors.focused_background = NULL; bar->colors.focused_background = NULL;
@ -1254,4 +1319,8 @@ struct bar_config *default_bar_config(void) {
list_add(config->bars, bar); list_add(config->bars, bar);
return bar; return bar;
cleanup:
free_bar(bar);
return NULL;
} }

@ -571,6 +571,10 @@ static void handle_binding_command(struct sway_binding *binding) {
// binding since it will be gone after the reload has completed. // binding since it will be gone after the reload has completed.
if (strcasecmp(binding->command, "reload") == 0) { if (strcasecmp(binding->command, "reload") == 0) {
binding_copy = sway_binding_dup(binding); binding_copy = sway_binding_dup(binding);
if (!binding_copy) {
sway_log(L_ERROR, "Unable to duplicate binding during reload");
return;
}
reload = true; reload = true;
} }

Loading…
Cancel
Save