mirror of
https://github.com/swaywm/sway.git
synced 2024-11-14 06:24:20 +01:00
Handle allocation failure in commands
This commit is contained in:
parent
8691ff1b63
commit
248df18c24
@ -386,7 +386,11 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) {
|
|||||||
if (!results) {
|
if (!results) {
|
||||||
int len = strlen(criteria) + strlen(head) + 4;
|
int len = strlen(criteria) + strlen(head) + 4;
|
||||||
char *tmp = malloc(len);
|
char *tmp = malloc(len);
|
||||||
|
if (tmp) {
|
||||||
snprintf(tmp, len, "[%s] %s", criteria, head);
|
snprintf(tmp, len, "[%s] %s", criteria, head);
|
||||||
|
} else {
|
||||||
|
sway_log(L_DEBUG, "Unable to allocate criteria string for cmd result");
|
||||||
|
}
|
||||||
results = cmd_results_new(CMD_INVALID, tmp,
|
results = cmd_results_new(CMD_INVALID, tmp,
|
||||||
"Can't handle criteria string: Refusing to execute command");
|
"Can't handle criteria string: Refusing to execute command");
|
||||||
free(tmp);
|
free(tmp);
|
||||||
@ -584,6 +588,10 @@ cleanup:
|
|||||||
|
|
||||||
struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) {
|
struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) {
|
||||||
struct cmd_results *results = malloc(sizeof(struct cmd_results));
|
struct cmd_results *results = malloc(sizeof(struct cmd_results));
|
||||||
|
if (!results) {
|
||||||
|
sway_log(L_ERROR, "Unable to allocate command results");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
results->status = status;
|
results->status = status;
|
||||||
if (input) {
|
if (input) {
|
||||||
results->input = strdup(input); // input is the command name
|
results->input = strdup(input); // input is the command name
|
||||||
@ -594,7 +602,9 @@ struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, c
|
|||||||
char *error = malloc(256);
|
char *error = malloc(256);
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
if (error) {
|
||||||
vsnprintf(error, 256, format, args);
|
vsnprintf(error, 256, format, args);
|
||||||
|
}
|
||||||
va_end(args);
|
va_end(args);
|
||||||
results->error = error;
|
results->error = error;
|
||||||
} else {
|
} else {
|
||||||
|
@ -27,6 +27,9 @@ struct cmd_results *cmd_assign(int argc, char **argv) {
|
|||||||
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) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria");
|
||||||
|
}
|
||||||
crit->crit_raw = strdup(criteria);
|
crit->crit_raw = strdup(criteria);
|
||||||
crit->cmdlist = cmdlist;
|
crit->cmdlist = cmdlist;
|
||||||
crit->tokens = create_list();
|
crit->tokens = create_list();
|
||||||
|
@ -32,6 +32,9 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
|
|||||||
|
|
||||||
// Create new bar with default values
|
// Create new bar with default values
|
||||||
struct bar_config *bar = default_bar_config();
|
struct bar_config *bar = default_bar_config();
|
||||||
|
if (!bar) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar state");
|
||||||
|
}
|
||||||
|
|
||||||
// set bar id
|
// set bar id
|
||||||
int i;
|
int i;
|
||||||
@ -39,7 +42,11 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
|
|||||||
if (bar == config->bars->items[i]) {
|
if (bar == config->bars->items[i]) {
|
||||||
const int len = 5 + numlen(i); // "bar-" + i + \0
|
const int len = 5 + numlen(i); // "bar-" + i + \0
|
||||||
bar->id = malloc(len * sizeof(char));
|
bar->id = malloc(len * sizeof(char));
|
||||||
|
if (bar->id) {
|
||||||
snprintf(bar->id, len, "bar-%d", i);
|
snprintf(bar->id, len, "bar-%d", i);
|
||||||
|
} else {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar ID");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,9 @@ struct cmd_results *bar_cmd_bindsym(int argc, char **argv) {
|
|||||||
return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]);
|
return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]);
|
||||||
}
|
}
|
||||||
struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding));
|
struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding));
|
||||||
|
if (!binding) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding");
|
||||||
|
}
|
||||||
binding->button = numbutton;
|
binding->button = numbutton;
|
||||||
binding->command = join_args(argv + 1, argc - 1);
|
binding->command = join_args(argv + 1, argc - 1);
|
||||||
|
|
||||||
|
@ -9,6 +9,9 @@ static struct cmd_results *parse_single_color(char **color, const char *cmd_name
|
|||||||
|
|
||||||
if (!*color) {
|
if (!*color) {
|
||||||
*color = malloc(10);
|
*color = malloc(10);
|
||||||
|
if (!*color) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
error = add_color(cmd_name, *color, argv[0]);
|
error = add_color(cmd_name, *color, argv[0]);
|
||||||
@ -29,6 +32,9 @@ static struct cmd_results *parse_three_colors(char ***colors, const char *cmd_na
|
|||||||
for (i = 0; i < 3; i++) {
|
for (i = 0; i < 3; i++) {
|
||||||
if (!*colors[i]) {
|
if (!*colors[i]) {
|
||||||
*(colors[i]) = malloc(10);
|
*(colors[i]) = malloc(10);
|
||||||
|
if (!*(colors[i])) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
error = add_color(cmd_name, *(colors[i]), argv[i]);
|
error = add_color(cmd_name, *(colors[i]), argv[i]);
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -16,6 +16,10 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
|
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
|
||||||
|
if (!binding) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindsym",
|
||||||
|
"Unable to allocate binding");
|
||||||
|
}
|
||||||
binding->keys = create_list();
|
binding->keys = create_list();
|
||||||
binding->modifiers = 0;
|
binding->modifiers = 0;
|
||||||
binding->release = false;
|
binding->release = false;
|
||||||
@ -46,14 +50,21 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Check for xkb key
|
// Check for xkb key
|
||||||
xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE);
|
xkb_keysym_t sym = xkb_keysym_from_name(split->items[i],
|
||||||
|
XKB_KEYSYM_CASE_INSENSITIVE);
|
||||||
if (!sym) {
|
if (!sym) {
|
||||||
error = cmd_results_new(CMD_INVALID, "bindsym", "Unknown key '%s'", (char *)split->items[i]);
|
|
||||||
free_sway_binding(binding);
|
free_sway_binding(binding);
|
||||||
list_free(split);
|
free_flat_list(split);
|
||||||
return error;
|
return cmd_results_new(CMD_INVALID, "bindsym", "Unknown key '%s'",
|
||||||
|
(char *)split->items[i]);
|
||||||
}
|
}
|
||||||
xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
|
xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
|
||||||
|
if (!key) {
|
||||||
|
free_sway_binding(binding);
|
||||||
|
free_flat_list(split);
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindsym",
|
||||||
|
"Unable to allocate binding");
|
||||||
|
}
|
||||||
*key = sym;
|
*key = sym;
|
||||||
list_add(binding->keys, key);
|
list_add(binding->keys, key);
|
||||||
}
|
}
|
||||||
@ -82,6 +93,10 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
|
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
|
||||||
|
if (!binding) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindsym",
|
||||||
|
"Unable to allocate binding");
|
||||||
|
}
|
||||||
binding->keys = create_list();
|
binding->keys = create_list();
|
||||||
binding->modifiers = 0;
|
binding->modifiers = 0;
|
||||||
binding->release = false;
|
binding->release = false;
|
||||||
|
@ -39,6 +39,9 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
|
|||||||
|
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space
|
pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space
|
||||||
|
if (!child) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "exec_always", "Unable to allocate child pid");
|
||||||
|
}
|
||||||
// Fork process
|
// Fork process
|
||||||
if ((pid = fork()) == 0) {
|
if ((pid = fork()) == 0) {
|
||||||
// Fork child process again
|
// Fork child process again
|
||||||
@ -56,7 +59,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
|
|||||||
_exit(0); // Close child process
|
_exit(0); // Close child process
|
||||||
} else if (pid < 0) {
|
} else if (pid < 0) {
|
||||||
free(child);
|
free(child);
|
||||||
return cmd_results_new(CMD_FAILURE, "exec_always", "Command failed (sway could not fork).");
|
return cmd_results_new(CMD_FAILURE, "exec_always", "fork() failed");
|
||||||
}
|
}
|
||||||
close(fd[1]); // close write
|
close(fd[1]); // close write
|
||||||
ssize_t s = 0;
|
ssize_t s = 0;
|
||||||
@ -73,8 +76,6 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
|
|||||||
pw->pid = child;
|
pw->pid = child;
|
||||||
pw->workspace = strdup(ws->name);
|
pw->workspace = strdup(ws->name);
|
||||||
pid_workspace_add(pw);
|
pid_workspace_add(pw);
|
||||||
// TODO: keep track of this pid and open the corresponding view on the current workspace
|
|
||||||
// blocked pending feature in wlc
|
|
||||||
} else {
|
} else {
|
||||||
free(child);
|
free(child);
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,9 @@ struct cmd_results *cmd_for_window(int argc, char **argv) {
|
|||||||
char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1);
|
char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1);
|
||||||
|
|
||||||
struct criteria *crit = malloc(sizeof(struct criteria));
|
struct criteria *crit = malloc(sizeof(struct criteria));
|
||||||
|
if (!crit) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "for_window", "Unable to allocate criteria");
|
||||||
|
}
|
||||||
crit->crit_raw = strdup(criteria);
|
crit->crit_raw = strdup(criteria);
|
||||||
crit->cmdlist = cmdlist;
|
crit->cmdlist = cmdlist;
|
||||||
crit->tokens = create_list();
|
crit->tokens = create_list();
|
||||||
|
@ -30,7 +30,10 @@ struct cmd_results *cmd_mode(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
// Create mode if it doesn't exist
|
// Create mode if it doesn't exist
|
||||||
if (!mode && mode_make) {
|
if (!mode && mode_make) {
|
||||||
mode = malloc(sizeof*mode);
|
mode = malloc(sizeof(struct sway_mode));
|
||||||
|
if (!mode) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "mode", "Unable to allocate mode");
|
||||||
|
}
|
||||||
mode->name = strdup(mode_name);
|
mode->name = strdup(mode_name);
|
||||||
mode->bindings = create_list();
|
mode->bindings = create_list();
|
||||||
list_add(config->modes, mode);
|
list_add(config->modes, mode);
|
||||||
|
@ -113,12 +113,20 @@ struct cmd_results *cmd_output(int argc, char **argv) {
|
|||||||
src = p.we_wordv[0];
|
src = p.we_wordv[0];
|
||||||
if (config->reading && *src != '/') {
|
if (config->reading && *src != '/') {
|
||||||
char *conf = strdup(config->current_config);
|
char *conf = strdup(config->current_config);
|
||||||
|
if (conf) {
|
||||||
char *conf_path = dirname(conf);
|
char *conf_path = dirname(conf);
|
||||||
src = malloc(strlen(conf_path) + strlen(src) + 2);
|
src = malloc(strlen(conf_path) + strlen(src) + 2);
|
||||||
|
if (src) {
|
||||||
sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
|
sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
|
||||||
free(conf);
|
} else {
|
||||||
|
sway_log(L_ERROR, "Unable to allocate background source");
|
||||||
}
|
}
|
||||||
if (access(src, F_OK) == -1) {
|
free(conf);
|
||||||
|
} else {
|
||||||
|
sway_log(L_ERROR, "Unable to allocate background source");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!src || access(src, F_OK) == -1) {
|
||||||
return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
|
return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
|
||||||
}
|
}
|
||||||
for (char *m = mode; *m; ++m) *m = tolower(*m);
|
for (char *m = mode; *m; ++m) *m = tolower(*m);
|
||||||
|
@ -47,6 +47,9 @@ struct cmd_results *cmd_set(int argc, char **argv) {
|
|||||||
free(var->value);
|
free(var->value);
|
||||||
} else {
|
} else {
|
||||||
var = malloc(sizeof(struct sway_variable));
|
var = malloc(sizeof(struct sway_variable));
|
||||||
|
if (!var) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "set", "Unable to allocate variable");
|
||||||
|
}
|
||||||
var->name = strdup(argv[0]);
|
var->name = strdup(argv[0]);
|
||||||
list_add(config->symbols, var);
|
list_add(config->symbols, var);
|
||||||
list_qsort(config->symbols, compare_set_qsort);
|
list_qsort(config->symbols, compare_set_qsort);
|
||||||
|
Loading…
Reference in New Issue
Block a user