bar: fix segfault with missing or invalid bar id

Prior to this patch, if I ran something like this, sway would crash:

    swaymsg bar height 50

or

    swaymsg bar not-a-bar-id color bg #ff0000

This was in contrast to other bar subcommands, like status_command,
which would exit with a "No bar defined" message.

The difference between the subcommands that crashed and the ones that
exited was that some subcommands had a check to see if a bar was
specified, while others just assumed that it had been and carried on
until they segfaulted.

Because this check was identical in every subcommand it was present in,
and I couldn't think of a case where it would be valid to run a bar
subcommand without specifying which bar to apply it to, I moved this
check from individual subcommands into the bar command, which is already
responsible for actually setting the specified bar. This reduced code
duplication, and fixed the crash for the subcommands that were missing
this check.
This commit is contained in:
Alyssa Ross 2019-05-16 22:56:58 +00:00 committed by Brian Ashworth
parent 578c1956ac
commit 5fb5984e94
20 changed files with 27 additions and 83 deletions

View file

@ -82,26 +82,30 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
++argv; --argc;
}
if (!config->current_bar && config->reading) {
// Create new bar with default values
struct bar_config *bar = default_bar_config();
if (!bar) {
return cmd_results_new(CMD_FAILURE,
"Unable to allocate bar state");
}
if (!config->current_bar) {
if (config->reading) {
// Create new bar with default values
struct bar_config *bar = default_bar_config();
if (!bar) {
return cmd_results_new(CMD_FAILURE,
"Unable to allocate bar state");
}
// set bar id
const int len = snprintf(NULL, 0, "bar-%d", config->bars->length - 1) + 1;
bar->id = malloc(len * sizeof(char));
if (bar->id) {
snprintf(bar->id, len, "bar-%d", config->bars->length - 1);
// set bar id
int len = snprintf(NULL, 0, "bar-%d", config->bars->length - 1) + 1;
bar->id = malloc(len * sizeof(char));
if (bar->id) {
snprintf(bar->id, len, "bar-%d", config->bars->length - 1);
} else {
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar ID");
}
// Set current bar
config->current_bar = bar;
sway_log(SWAY_DEBUG, "Creating bar %s", bar->id);
} else {
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar ID");
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
// Set current bar
config->current_bar = bar;
sway_log(SWAY_DEBUG, "Creating bar %s", bar->id);
}
if (find_handler(argv[0], bar_config_handlers,

View file

@ -75,9 +75,6 @@ static struct cmd_results *bar_cmd_bind(int argc, char **argv, bool code,
if ((error = checkarg(argc, command, EXPECTED_AT_LEAST, minargs))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
struct bar_binding *binding = calloc(1, sizeof(struct bar_binding));
if (!binding) {

View file

@ -10,9 +10,6 @@ struct cmd_results *bar_cmd_binding_mode_indicator(int argc, char **argv) {
"binding_mode_indicator", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
config->current_bar->binding_mode_indicator =
parse_boolean(argv[0], config->current_bar->binding_mode_indicator);
if (config->current_bar->binding_mode_indicator) {

View file

@ -9,9 +9,6 @@ struct cmd_results *bar_cmd_font(int argc, char **argv) {
if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
char *font = join_args(argv, argc);
free(config->current_bar->font);
config->current_bar->font = font;

View file

@ -13,9 +13,6 @@ struct cmd_results *bar_cmd_gaps(int argc, char **argv) {
if ((error = checkarg(argc, "gaps", EXPECTED_AT_MOST, 4))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
int top = 0, right = 0, bottom = 0, left = 0;

View file

@ -12,10 +12,6 @@ struct cmd_results *bar_cmd_icon_theme(int argc, char **argv) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
sway_log(SWAY_DEBUG, "[Bar %s] Setting icon theme to %s",
config->current_bar->id, argv[0]);
free(config->current_bar->icon_theme);

View file

@ -10,10 +10,6 @@ struct cmd_results *bar_cmd_modifier(int argc, char **argv) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
uint32_t mod = 0;
if (strcmp(argv[0], "none") != 0) {
list_t *split = split_string(argv[0], "+");

View file

@ -10,9 +10,6 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) {
if ((error = checkarg(argc, "output", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
const char *output = argv[0];
list_t *outputs = config->current_bar->outputs;

View file

@ -9,11 +9,8 @@ struct cmd_results *bar_cmd_pango_markup(int argc, char **argv) {
if ((error = checkarg(argc, "pango_markup", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
config->current_bar->pango_markup
= parse_boolean(argv[0], config->current_bar->pango_markup);
config->current_bar->pango_markup =
parse_boolean(argv[0], config->current_bar->pango_markup);
if (config->current_bar->pango_markup) {
sway_log(SWAY_DEBUG, "Enabling pango markup for bar: %s",
config->current_bar->id);

View file

@ -9,9 +9,6 @@ struct cmd_results *bar_cmd_position(int argc, char **argv) {
if ((error = checkarg(argc, "position", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
char *valid[] = { "top", "bottom" };
for (size_t i = 0; i < sizeof(valid) / sizeof(valid[0]); ++i) {
if (strcasecmp(valid[i], argv[0]) == 0) {

View file

@ -8,9 +8,6 @@ struct cmd_results *bar_cmd_separator_symbol(int argc, char **argv) {
if ((error = checkarg(argc, "separator_symbol", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
free(config->current_bar->separator_symbol);
config->current_bar->separator_symbol = strdup(argv[0]);
sway_log(SWAY_DEBUG, "Settings separator_symbol '%s' for bar: %s",

View file

@ -8,9 +8,6 @@ struct cmd_results *bar_cmd_status_command(int argc, char **argv) {
if ((error = checkarg(argc, "status_command", EXPECTED_AT_LEAST, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
free(config->current_bar->status_command);
config->current_bar->status_command = NULL;

View file

@ -10,9 +10,6 @@ struct cmd_results *bar_cmd_strip_workspace_name(int argc, char **argv) {
"strip_workspace_name", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
config->current_bar->strip_workspace_name =
parse_boolean(argv[0], config->current_bar->strip_workspace_name);

View file

@ -10,9 +10,6 @@ struct cmd_results *bar_cmd_strip_workspace_numbers(int argc, char **argv) {
"strip_workspace_numbers", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
config->current_bar->strip_workspace_numbers =
parse_boolean(argv[0], config->current_bar->strip_workspace_numbers);

View file

@ -8,9 +8,6 @@ struct cmd_results *bar_cmd_swaybar_command(int argc, char **argv) {
if ((error = checkarg(argc, "swaybar_command", EXPECTED_AT_LEAST, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
free(config->current_bar->swaybar_command);
config->current_bar->swaybar_command = join_args(argv, argc);
sway_log(SWAY_DEBUG, "Using custom swaybar command: %s",

View file

@ -12,9 +12,6 @@ static struct cmd_results *tray_bind(int argc, char **argv, bool code) {
if ((error = checkarg(argc, command, EXPECTED_EQUAL_TO, 2))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
struct tray_binding *binding = calloc(1, sizeof(struct tray_binding));
if (!binding) {

View file

@ -13,10 +13,6 @@ struct cmd_results *bar_cmd_tray_output(int argc, char **argv) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
list_t *outputs = config->current_bar->tray_outputs;
if (!outputs) {
config->current_bar->tray_outputs = outputs = create_list();

View file

@ -15,9 +15,6 @@ struct cmd_results *bar_cmd_tray_padding(int argc, char **argv) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
struct bar_config *bar = config->current_bar;
char *end;

View file

@ -9,9 +9,6 @@ struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) {
if ((error = checkarg(argc, "workspace_buttons", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
config->current_bar->workspace_buttons =
parse_boolean(argv[0], config->current_bar->workspace_buttons);
if (config->current_bar->workspace_buttons) {

View file

@ -9,9 +9,6 @@ struct cmd_results *bar_cmd_wrap_scroll(int argc, char **argv) {
if ((error = checkarg(argc, "wrap_scroll", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
config->current_bar->wrap_scroll =
parse_boolean(argv[0], config->current_bar->wrap_scroll);
if (config->current_bar->wrap_scroll) {