Handle allocation failure in commands

This commit is contained in:
Drew DeVault 2016-12-15 17:39:09 -05:00
parent 8691ff1b63
commit 248df18c24
11 changed files with 78 additions and 16 deletions

View file

@ -386,7 +386,11 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) {
if (!results) {
int len = strlen(criteria) + strlen(head) + 4;
char *tmp = malloc(len);
snprintf(tmp, len, "[%s] %s", criteria, head);
if (tmp) {
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,
"Can't handle criteria string: Refusing to execute command");
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 *results = malloc(sizeof(struct cmd_results));
if (!results) {
sway_log(L_ERROR, "Unable to allocate command results");
return NULL;
}
results->status = status;
if (input) {
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);
va_list args;
va_start(args, format);
vsnprintf(error, 256, format, args);
if (error) {
vsnprintf(error, 256, format, args);
}
va_end(args);
results->error = error;
} else {