aboutsummaryrefslogtreecommitdiffstats
path: root/sway
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2016-12-15 17:39:09 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2016-12-15 19:01:40 -0500
commit248df18c24d2a849de984b477ca3913ce7c72441 (patch)
tree35beb85ecfcc54574d3b5e82c8445eb8c1219689 /sway
parentHandle border-related malloc failures (diff)
downloadsway-248df18c24d2a849de984b477ca3913ce7c72441.tar.gz
sway-248df18c24d2a849de984b477ca3913ce7c72441.tar.zst
sway-248df18c24d2a849de984b477ca3913ce7c72441.zip
Handle allocation failure in commands
Diffstat (limited to 'sway')
-rw-r--r--sway/commands.c14
-rw-r--r--sway/commands/assign.c3
-rw-r--r--sway/commands/bar.c9
-rw-r--r--sway/commands/bar/bindsym.c3
-rw-r--r--sway/commands/bar/colors.c6
-rw-r--r--sway/commands/bind.c23
-rw-r--r--sway/commands/exec_always.c7
-rw-r--r--sway/commands/for_window.c3
-rw-r--r--sway/commands/mode.c5
-rw-r--r--sway/commands/output.c18
-rw-r--r--sway/commands/set.c3
11 files changed, 78 insertions, 16 deletions
diff --git a/sway/commands.c b/sway/commands.c
index d87d0084..dee03d71 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -386,7 +386,11 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) {
386 if (!results) { 386 if (!results) {
387 int len = strlen(criteria) + strlen(head) + 4; 387 int len = strlen(criteria) + strlen(head) + 4;
388 char *tmp = malloc(len); 388 char *tmp = malloc(len);
389 snprintf(tmp, len, "[%s] %s", criteria, head); 389 if (tmp) {
390 snprintf(tmp, len, "[%s] %s", criteria, head);
391 } else {
392 sway_log(L_DEBUG, "Unable to allocate criteria string for cmd result");
393 }
390 results = cmd_results_new(CMD_INVALID, tmp, 394 results = cmd_results_new(CMD_INVALID, tmp,
391 "Can't handle criteria string: Refusing to execute command"); 395 "Can't handle criteria string: Refusing to execute command");
392 free(tmp); 396 free(tmp);
@@ -584,6 +588,10 @@ cleanup:
584 588
585struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) { 589struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) {
586 struct cmd_results *results = malloc(sizeof(struct cmd_results)); 590 struct cmd_results *results = malloc(sizeof(struct cmd_results));
591 if (!results) {
592 sway_log(L_ERROR, "Unable to allocate command results");
593 return NULL;
594 }
587 results->status = status; 595 results->status = status;
588 if (input) { 596 if (input) {
589 results->input = strdup(input); // input is the command name 597 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
594 char *error = malloc(256); 602 char *error = malloc(256);
595 va_list args; 603 va_list args;
596 va_start(args, format); 604 va_start(args, format);
597 vsnprintf(error, 256, format, args); 605 if (error) {
606 vsnprintf(error, 256, format, args);
607 }
598 va_end(args); 608 va_end(args);
599 results->error = error; 609 results->error = error;
600 } else { 610 } else {
diff --git a/sway/commands/assign.c b/sway/commands/assign.c
index 53c599ca..1824692b 100644
--- a/sway/commands/assign.c
+++ b/sway/commands/assign.c
@@ -27,6 +27,9 @@ struct cmd_results *cmd_assign(int argc, char **argv) {
27 snprintf(cmdlist, arglen, "%s%s", movecmd, *argv); 27 snprintf(cmdlist, arglen, "%s%s", movecmd, *argv);
28 28
29 struct criteria *crit = malloc(sizeof(struct criteria)); 29 struct criteria *crit = malloc(sizeof(struct criteria));
30 if (!crit) {
31 return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria");
32 }
30 crit->crit_raw = strdup(criteria); 33 crit->crit_raw = strdup(criteria);
31 crit->cmdlist = cmdlist; 34 crit->cmdlist = cmdlist;
32 crit->tokens = create_list(); 35 crit->tokens = create_list();
diff --git a/sway/commands/bar.c b/sway/commands/bar.c
index 55cb0d9d..e8d24084 100644
--- a/sway/commands/bar.c
+++ b/sway/commands/bar.c
@@ -32,6 +32,9 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
32 32
33 // Create new bar with default values 33 // Create new bar with default values
34 struct bar_config *bar = default_bar_config(); 34 struct bar_config *bar = default_bar_config();
35 if (!bar) {
36 return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar state");
37 }
35 38
36 // set bar id 39 // set bar id
37 int i; 40 int i;
@@ -39,7 +42,11 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
39 if (bar == config->bars->items[i]) { 42 if (bar == config->bars->items[i]) {
40 const int len = 5 + numlen(i); // "bar-" + i + \0 43 const int len = 5 + numlen(i); // "bar-" + i + \0
41 bar->id = malloc(len * sizeof(char)); 44 bar->id = malloc(len * sizeof(char));
42 snprintf(bar->id, len, "bar-%d", i); 45 if (bar->id) {
46 snprintf(bar->id, len, "bar-%d", i);
47 } else {
48 return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar ID");
49 }
43 break; 50 break;
44 } 51 }
45 } 52 }
diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c
index bb81b4a9..5f90b51a 100644
--- a/sway/commands/bar/bindsym.c
+++ b/sway/commands/bar/bindsym.c
@@ -26,6 +26,9 @@ struct cmd_results *bar_cmd_bindsym(int argc, char **argv) {
26 return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]); 26 return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]);
27 } 27 }
28 struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding)); 28 struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding));
29 if (!binding) {
30 return cmd_results_new(CMD_FAILURE, "bindsym", "Unable to allocate binding");
31 }
29 binding->button = numbutton; 32 binding->button = numbutton;
30 binding->command = join_args(argv + 1, argc - 1); 33 binding->command = join_args(argv + 1, argc - 1);
31 34
diff --git a/sway/commands/bar/colors.c b/sway/commands/bar/colors.c
index f6fb520a..f8792973 100644
--- a/sway/commands/bar/colors.c
+++ b/sway/commands/bar/colors.c
@@ -9,6 +9,9 @@ static struct cmd_results *parse_single_color(char **color, const char *cmd_name
9 9
10 if (!*color) { 10 if (!*color) {
11 *color = malloc(10); 11 *color = malloc(10);
12 if (!*color) {
13 return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color");
14 }
12 } 15 }
13 16
14 error = add_color(cmd_name, *color, argv[0]); 17 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
29 for (i = 0; i < 3; i++) { 32 for (i = 0; i < 3; i++) {
30 if (!*colors[i]) { 33 if (!*colors[i]) {
31 *(colors[i]) = malloc(10); 34 *(colors[i]) = malloc(10);
35 if (!*(colors[i])) {
36 return cmd_results_new(CMD_FAILURE, cmd_name, "Unable to allocate color");
37 }
32 } 38 }
33 error = add_color(cmd_name, *(colors[i]), argv[i]); 39 error = add_color(cmd_name, *(colors[i]), argv[i]);
34 if (error) { 40 if (error) {
diff --git a/sway/commands/bind.c b/sway/commands/bind.c
index e8bb3ee8..8282277b 100644
--- a/sway/commands/bind.c
+++ b/sway/commands/bind.c
@@ -16,6 +16,10 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) {
16 } 16 }
17 17
18 struct sway_binding *binding = malloc(sizeof(struct sway_binding)); 18 struct sway_binding *binding = malloc(sizeof(struct sway_binding));
19 if (!binding) {
20 return cmd_results_new(CMD_FAILURE, "bindsym",
21 "Unable to allocate binding");
22 }
19 binding->keys = create_list(); 23 binding->keys = create_list();
20 binding->modifiers = 0; 24 binding->modifiers = 0;
21 binding->release = false; 25 binding->release = false;
@@ -46,14 +50,21 @@ struct cmd_results *cmd_bindsym(int argc, char **argv) {
46 continue; 50 continue;
47 } 51 }
48 // Check for xkb key 52 // Check for xkb key
49 xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE); 53 xkb_keysym_t sym = xkb_keysym_from_name(split->items[i],
54 XKB_KEYSYM_CASE_INSENSITIVE);
50 if (!sym) { 55 if (!sym) {
51 error = cmd_results_new(CMD_INVALID, "bindsym", "Unknown key '%s'", (char *)split->items[i]);
52 free_sway_binding(binding); 56 free_sway_binding(binding);
53 list_free(split); 57 free_flat_list(split);
54 return error; 58 return cmd_results_new(CMD_INVALID, "bindsym", "Unknown key '%s'",
59 (char *)split->items[i]);
55 } 60 }
56 xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); 61 xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
62 if (!key) {
63 free_sway_binding(binding);
64 free_flat_list(split);
65 return cmd_results_new(CMD_FAILURE, "bindsym",
66 "Unable to allocate binding");
67 }
57 *key = sym; 68 *key = sym;
58 list_add(binding->keys, key); 69 list_add(binding->keys, key);
59 } 70 }
@@ -82,6 +93,10 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
82 } 93 }
83 94
84 struct sway_binding *binding = malloc(sizeof(struct sway_binding)); 95 struct sway_binding *binding = malloc(sizeof(struct sway_binding));
96 if (!binding) {
97 return cmd_results_new(CMD_FAILURE, "bindsym",
98 "Unable to allocate binding");
99 }
85 binding->keys = create_list(); 100 binding->keys = create_list();
86 binding->modifiers = 0; 101 binding->modifiers = 0;
87 binding->release = false; 102 binding->release = false;
diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c
index 157d4872..1d7cd494 100644
--- a/sway/commands/exec_always.c
+++ b/sway/commands/exec_always.c
@@ -39,6 +39,9 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
39 39
40 pid_t pid; 40 pid_t pid;
41 pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space 41 pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space
42 if (!child) {
43 return cmd_results_new(CMD_FAILURE, "exec_always", "Unable to allocate child pid");
44 }
42 // Fork process 45 // Fork process
43 if ((pid = fork()) == 0) { 46 if ((pid = fork()) == 0) {
44 // Fork child process again 47 // Fork child process again
@@ -56,7 +59,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
56 _exit(0); // Close child process 59 _exit(0); // Close child process
57 } else if (pid < 0) { 60 } else if (pid < 0) {
58 free(child); 61 free(child);
59 return cmd_results_new(CMD_FAILURE, "exec_always", "Command failed (sway could not fork)."); 62 return cmd_results_new(CMD_FAILURE, "exec_always", "fork() failed");
60 } 63 }
61 close(fd[1]); // close write 64 close(fd[1]); // close write
62 ssize_t s = 0; 65 ssize_t s = 0;
@@ -73,8 +76,6 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
73 pw->pid = child; 76 pw->pid = child;
74 pw->workspace = strdup(ws->name); 77 pw->workspace = strdup(ws->name);
75 pid_workspace_add(pw); 78 pid_workspace_add(pw);
76 // TODO: keep track of this pid and open the corresponding view on the current workspace
77 // blocked pending feature in wlc
78 } else { 79 } else {
79 free(child); 80 free(child);
80 } 81 }
diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c
index 2ba0ea6c..8c5722fd 100644
--- a/sway/commands/for_window.c
+++ b/sway/commands/for_window.c
@@ -14,6 +14,9 @@ struct cmd_results *cmd_for_window(int argc, char **argv) {
14 char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1); 14 char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1);
15 15
16 struct criteria *crit = malloc(sizeof(struct criteria)); 16 struct criteria *crit = malloc(sizeof(struct criteria));
17 if (!crit) {
18 return cmd_results_new(CMD_FAILURE, "for_window", "Unable to allocate criteria");
19 }
17 crit->crit_raw = strdup(criteria); 20 crit->crit_raw = strdup(criteria);
18 crit->cmdlist = cmdlist; 21 crit->cmdlist = cmdlist;
19 crit->tokens = create_list(); 22 crit->tokens = create_list();
diff --git a/sway/commands/mode.c b/sway/commands/mode.c
index f9cb271e..ed3f432f 100644
--- a/sway/commands/mode.c
+++ b/sway/commands/mode.c
@@ -30,7 +30,10 @@ struct cmd_results *cmd_mode(int argc, char **argv) {
30 } 30 }
31 // Create mode if it doesn't exist 31 // Create mode if it doesn't exist
32 if (!mode && mode_make) { 32 if (!mode && mode_make) {
33 mode = malloc(sizeof*mode); 33 mode = malloc(sizeof(struct sway_mode));
34 if (!mode) {
35 return cmd_results_new(CMD_FAILURE, "mode", "Unable to allocate mode");
36 }
34 mode->name = strdup(mode_name); 37 mode->name = strdup(mode_name);
35 mode->bindings = create_list(); 38 mode->bindings = create_list();
36 list_add(config->modes, mode); 39 list_add(config->modes, mode);
diff --git a/sway/commands/output.c b/sway/commands/output.c
index 6c1c55b5..e150aed2 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -113,12 +113,20 @@ struct cmd_results *cmd_output(int argc, char **argv) {
113 src = p.we_wordv[0]; 113 src = p.we_wordv[0];
114 if (config->reading && *src != '/') { 114 if (config->reading && *src != '/') {
115 char *conf = strdup(config->current_config); 115 char *conf = strdup(config->current_config);
116 char *conf_path = dirname(conf); 116 if (conf) {
117 src = malloc(strlen(conf_path) + strlen(src) + 2); 117 char *conf_path = dirname(conf);
118 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); 118 src = malloc(strlen(conf_path) + strlen(src) + 2);
119 free(conf); 119 if (src) {
120 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
121 } else {
122 sway_log(L_ERROR, "Unable to allocate background source");
123 }
124 free(conf);
125 } else {
126 sway_log(L_ERROR, "Unable to allocate background source");
127 }
120 } 128 }
121 if (access(src, F_OK) == -1) { 129 if (!src || access(src, F_OK) == -1) {
122 return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src); 130 return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
123 } 131 }
124 for (char *m = mode; *m; ++m) *m = tolower(*m); 132 for (char *m = mode; *m; ++m) *m = tolower(*m);
diff --git a/sway/commands/set.c b/sway/commands/set.c
index 1f324951..8b293825 100644
--- a/sway/commands/set.c
+++ b/sway/commands/set.c
@@ -47,6 +47,9 @@ struct cmd_results *cmd_set(int argc, char **argv) {
47 free(var->value); 47 free(var->value);
48 } else { 48 } else {
49 var = malloc(sizeof(struct sway_variable)); 49 var = malloc(sizeof(struct sway_variable));
50 if (!var) {
51 return cmd_results_new(CMD_FAILURE, "set", "Unable to allocate variable");
52 }
50 var->name = strdup(argv[0]); 53 var->name = strdup(argv[0]);
51 list_add(config->symbols, var); 54 list_add(config->symbols, var);
52 list_qsort(config->symbols, compare_set_qsort); 55 list_qsort(config->symbols, compare_set_qsort);