summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2016-12-17 13:23:44 -0500
committerLibravatar GitHub <noreply@github.com>2016-12-17 13:23:44 -0500
commitf04ee0e68d885d7e1101cc88f9a9337202041f1f (patch)
treefa4dc296a5f1377867752d320ceef4e4b0178bbf
parentMerge pull request #991 from barfoo1/registry_fix (diff)
parentFix build error (diff)
downloadsway-f04ee0e68d885d7e1101cc88f9a9337202041f1f.tar.gz
sway-f04ee0e68d885d7e1101cc88f9a9337202041f1f.tar.zst
sway-f04ee0e68d885d7e1101cc88f9a9337202041f1f.zip
Merge pull request #995 from SirCmpwn/memory-use
Handle allocation failures
-rw-r--r--common/ipc-client.c13
-rw-r--r--common/list.c3
-rw-r--r--common/log.c11
-rw-r--r--common/readline.c3
-rw-r--r--include/log.h5
-rw-r--r--sway/border.c29
-rw-r--r--sway/commands.c20
-rw-r--r--sway/commands/assign.c8
-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.c21
-rw-r--r--sway/commands/permit.c3
-rw-r--r--sway/commands/set.c3
-rw-r--r--sway/commands/workspace.c4
-rw-r--r--sway/config.c195
-rw-r--r--sway/container.c5
-rw-r--r--sway/extensions.c8
-rw-r--r--sway/handlers.c13
-rw-r--r--sway/input.c14
-rw-r--r--sway/ipc-json.c9
-rw-r--r--sway/ipc-server.c33
-rw-r--r--sway/main.c15
-rw-r--r--sway/security.c20
-rw-r--r--sway/workspace.c10
-rw-r--r--swaybar/ipc.c3
30 files changed, 401 insertions, 103 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c
index 106f9d86..d011bd26 100644
--- a/common/ipc-client.c
+++ b/common/ipc-client.c
@@ -52,10 +52,18 @@ struct ipc_response *ipc_recv_response(int socketfd) {
52 } 52 }
53 53
54 struct ipc_response *response = malloc(sizeof(struct ipc_response)); 54 struct ipc_response *response = malloc(sizeof(struct ipc_response));
55 if (!response) {
56 goto error_1;
57 }
58
55 total = 0; 59 total = 0;
56 response->size = data32[0]; 60 response->size = data32[0];
57 response->type = data32[1]; 61 response->type = data32[1];
58 char *payload = malloc(response->size + 1); 62 char *payload = malloc(response->size + 1);
63 if (!payload) {
64 goto error_2;
65 }
66
59 while (total < response->size) { 67 while (total < response->size) {
60 ssize_t received = recv(socketfd, payload + total, response->size - total, 0); 68 ssize_t received = recv(socketfd, payload + total, response->size - total, 0);
61 if (received < 0) { 69 if (received < 0) {
@@ -67,6 +75,11 @@ struct ipc_response *ipc_recv_response(int socketfd) {
67 response->payload = payload; 75 response->payload = payload;
68 76
69 return response; 77 return response;
78error_2:
79 free(response);
80error_1:
81 sway_log(L_ERROR, "Unable to allocate memory for IPC response");
82 return NULL;
70} 83}
71 84
72void free_ipc_response(struct ipc_response *response) { 85void free_ipc_response(struct ipc_response *response) {
diff --git a/common/list.c b/common/list.c
index d57234e3..dd864a9b 100644
--- a/common/list.c
+++ b/common/list.c
@@ -5,6 +5,9 @@
5 5
6list_t *create_list(void) { 6list_t *create_list(void) {
7 list_t *list = malloc(sizeof(list_t)); 7 list_t *list = malloc(sizeof(list_t));
8 if (!list) {
9 return NULL;
10 }
8 list->capacity = 10; 11 list->capacity = 10;
9 list->length = 0; 12 list->length = 0;
10 list->items = malloc(sizeof(void*) * list->capacity); 13 list->items = malloc(sizeof(void*) * list->capacity);
diff --git a/common/log.c b/common/log.c
index 4f0baa3f..825b176b 100644
--- a/common/log.c
+++ b/common/log.c
@@ -88,9 +88,14 @@ void _sway_log(const char *filename, int line, log_importance_t verbosity, const
88 } 88 }
89 89
90 if (filename && line) { 90 if (filename && line) {
91 char *file = strdup(filename); 91 const char *file = filename + strlen(filename);
92 fprintf(stderr, "[%s:%d] ", basename(file), line); 92 while (file != filename && *file != '/') {
93 free(file); 93 --file;
94 }
95 if (*file == '/') {
96 ++file;
97 }
98 fprintf(stderr, "[%s:%d] ", file, line);
94 } 99 }
95 100
96 va_list args; 101 va_list args;
diff --git a/common/readline.c b/common/readline.c
index 5106172c..cc40a2cc 100644
--- a/common/readline.c
+++ b/common/readline.c
@@ -1,4 +1,5 @@
1#include "readline.h" 1#include "readline.h"
2#include "log.h"
2#include <stdlib.h> 3#include <stdlib.h>
3#include <stdio.h> 4#include <stdio.h>
4 5
@@ -7,6 +8,7 @@ char *read_line(FILE *file) {
7 char *string = malloc(size); 8 char *string = malloc(size);
8 char lastChar = '\0'; 9 char lastChar = '\0';
9 if (!string) { 10 if (!string) {
11 sway_log(L_ERROR, "Unable to allocate memory for read_line");
10 return NULL; 12 return NULL;
11 } 13 }
12 while (1) { 14 while (1) {
@@ -27,6 +29,7 @@ char *read_line(FILE *file) {
27 char *new_string = realloc(string, size *= 2); 29 char *new_string = realloc(string, size *= 2);
28 if (!new_string) { 30 if (!new_string) {
29 free(string); 31 free(string);
32 sway_log(L_ERROR, "Unable to allocate memory for read_line");
30 return NULL; 33 return NULL;
31 } 34 }
32 string = new_string; 35 string = new_string;
diff --git a/include/log.h b/include/log.h
index ca8c1fe3..2c4150e4 100644
--- a/include/log.h
+++ b/include/log.h
@@ -25,13 +25,8 @@ bool _sway_assert(bool condition, const char* format, ...) __attribute__((format
25 25
26void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) __attribute__((format(printf,4,5))); 26void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) __attribute__((format(printf,4,5)));
27 27
28#ifndef NDEBUG
29#define sway_log(VERBOSITY, FMT, ...) \ 28#define sway_log(VERBOSITY, FMT, ...) \
30 _sway_log(__FILE__, __LINE__, VERBOSITY, FMT, ##__VA_ARGS__) 29 _sway_log(__FILE__, __LINE__, VERBOSITY, FMT, ##__VA_ARGS__)
31#else
32#define sway_log(VERBOSITY, FMT, ...) \
33 _sway_log(NULL, 0, VERBOSITY, FMT, ##__VA_ARGS__)
34#endif
35 30
36void error_handler(int sig); 31void error_handler(int sig);
37 32
diff --git a/sway/border.c b/sway/border.c
index c96ae6fe..5ae32d24 100644
--- a/sway/border.c
+++ b/sway/border.c
@@ -29,20 +29,24 @@ void border_clear(struct border *border) {
29static cairo_t *create_border_buffer(swayc_t *view, struct wlc_geometry g, cairo_surface_t **surface) { 29static cairo_t *create_border_buffer(swayc_t *view, struct wlc_geometry g, cairo_surface_t **surface) {
30 if (view->border == NULL) { 30 if (view->border == NULL) {
31 view->border = malloc(sizeof(struct border)); 31 view->border = malloc(sizeof(struct border));
32 if (!view->border) {
33 sway_log(L_ERROR, "Unable to allocate window border information");
34 return NULL;
35 }
32 } 36 }
33 cairo_t *cr; 37 cairo_t *cr;
34 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, g.size.w); 38 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, g.size.w);
35 view->border->buffer = calloc(stride * g.size.h, sizeof(unsigned char)); 39 view->border->buffer = calloc(stride * g.size.h, sizeof(unsigned char));
36 view->border->geometry = g; 40 view->border->geometry = g;
37 if (!view->border->buffer) { 41 if (!view->border->buffer) {
38 sway_log(L_DEBUG, "Unable to allocate buffer"); 42 sway_log(L_ERROR, "Unable to allocate window border buffer");
39 return NULL; 43 return NULL;
40 } 44 }
41 *surface = cairo_image_surface_create_for_data(view->border->buffer, 45 *surface = cairo_image_surface_create_for_data(view->border->buffer,
42 CAIRO_FORMAT_ARGB32, g.size.w, g.size.h, stride); 46 CAIRO_FORMAT_ARGB32, g.size.w, g.size.h, stride);
43 if (cairo_surface_status(*surface) != CAIRO_STATUS_SUCCESS) { 47 if (cairo_surface_status(*surface) != CAIRO_STATUS_SUCCESS) {
44 border_clear(view->border); 48 border_clear(view->border);
45 sway_log(L_DEBUG, "Unable to allocate surface"); 49 sway_log(L_ERROR, "Unable to allocate window border surface");
46 return NULL; 50 return NULL;
47 } 51 }
48 cr = cairo_create(*surface); 52 cr = cairo_create(*surface);
@@ -50,7 +54,7 @@ static cairo_t *create_border_buffer(swayc_t *view, struct wlc_geometry g, cairo
50 if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) { 54 if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
51 cairo_surface_destroy(*surface); 55 cairo_surface_destroy(*surface);
52 border_clear(view->border); 56 border_clear(view->border);
53 sway_log(L_DEBUG, "Unable to create cairo context"); 57 sway_log(L_ERROR, "Unable to create cairo context");
54 return NULL; 58 return NULL;
55 } 59 }
56 return cr; 60 return cr;
@@ -238,6 +242,10 @@ static char *generate_container_title(swayc_t *container) {
238 } 242 }
239 int len = 9; 243 int len = 9;
240 name = malloc(len * sizeof(char)); 244 name = malloc(len * sizeof(char));
245 if (!name) {
246 sway_log(L_ERROR, "Unable to allocate container title");
247 return NULL;
248 }
241 snprintf(name, len, "sway: %c[", layout); 249 snprintf(name, len, "sway: %c[", layout);
242 250
243 int i; 251 int i;
@@ -257,6 +265,11 @@ static char *generate_container_title(swayc_t *container) {
257 } 265 }
258 266
259 name = malloc(len * sizeof(char)); 267 name = malloc(len * sizeof(char));
268 if (!name) {
269 free(prev_name);
270 sway_log(L_ERROR, "Unable to allocate container title");
271 return NULL;
272 }
260 if (i < container->children->length-1) { 273 if (i < container->children->length-1) {
261 snprintf(name, len, "%s%s ", prev_name, title); 274 snprintf(name, len, "%s%s ", prev_name, title);
262 } else { 275 } else {
@@ -268,6 +281,11 @@ static char *generate_container_title(swayc_t *container) {
268 prev_name = name; 281 prev_name = name;
269 len = strlen(name) + 2; 282 len = strlen(name) + 2;
270 name = malloc(len * sizeof(char)); 283 name = malloc(len * sizeof(char));
284 if (!name) {
285 free(prev_name);
286 sway_log(L_ERROR, "Unable to allocate container title");
287 return NULL;
288 }
271 snprintf(name, len, "%s]", prev_name); 289 snprintf(name, len, "%s]", prev_name);
272 free(prev_name); 290 free(prev_name);
273 free(container->name); 291 free(container->name);
@@ -347,6 +365,9 @@ static void update_view_border(swayc_t *view) {
347 } 365 }
348 }; 366 };
349 cr = create_border_buffer(view, g, &surface); 367 cr = create_border_buffer(view, g, &surface);
368 if (!cr) {
369 goto cleanup;
370 }
350 371
351 bool render_top = !should_hide_top_border(view, view->y); 372 bool render_top = !should_hide_top_border(view, view->y);
352 if (view == focused || is_child_of_focused) { 373 if (view == focused || is_child_of_focused) {
@@ -408,6 +429,8 @@ static void update_view_border(swayc_t *view) {
408 } 429 }
409 } 430 }
410 431
432cleanup:
433
411 if (surface) { 434 if (surface) {
412 cairo_surface_flush(surface); 435 cairo_surface_flush(surface);
413 cairo_surface_destroy(surface); 436 cairo_surface_destroy(surface);
diff --git a/sway/commands.c b/sway/commands.c
index d87d0084..c15cb00a 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -121,6 +121,9 @@ void input_cmd_apply(struct input_config *input) {
121 for (int i = 0; i < input_devices->length; ++i) { 121 for (int i = 0; i < input_devices->length; ++i) {
122 device = input_devices->items[i]; 122 device = input_devices->items[i];
123 char* dev_identifier = libinput_dev_unique_id(device); 123 char* dev_identifier = libinput_dev_unique_id(device);
124 if (!dev_identifier) {
125 break;
126 }
124 int match = dev_identifier && strcmp(dev_identifier, input->identifier) == 0; 127 int match = dev_identifier && strcmp(dev_identifier, input->identifier) == 0;
125 free(dev_identifier); 128 free(dev_identifier);
126 if (match) { 129 if (match) {
@@ -386,7 +389,11 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) {
386 if (!results) { 389 if (!results) {
387 int len = strlen(criteria) + strlen(head) + 4; 390 int len = strlen(criteria) + strlen(head) + 4;
388 char *tmp = malloc(len); 391 char *tmp = malloc(len);
389 snprintf(tmp, len, "[%s] %s", criteria, head); 392 if (tmp) {
393 snprintf(tmp, len, "[%s] %s", criteria, head);
394 } else {
395 sway_log(L_DEBUG, "Unable to allocate criteria string for cmd result");
396 }
390 results = cmd_results_new(CMD_INVALID, tmp, 397 results = cmd_results_new(CMD_INVALID, tmp,
391 "Can't handle criteria string: Refusing to execute command"); 398 "Can't handle criteria string: Refusing to execute command");
392 free(tmp); 399 free(tmp);
@@ -568,6 +575,9 @@ struct cmd_results *config_commands_command(char *exec) {
568 } 575 }
569 if (!policy) { 576 if (!policy) {
570 policy = alloc_command_policy(cmd); 577 policy = alloc_command_policy(cmd);
578 if (!policy) {
579 sway_abort("Unable to allocate security policy");
580 }
571 list_add(config->command_policies, policy); 581 list_add(config->command_policies, policy);
572 } 582 }
573 policy->context = context; 583 policy->context = context;
@@ -584,6 +594,10 @@ cleanup:
584 594
585struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) { 595struct 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)); 596 struct cmd_results *results = malloc(sizeof(struct cmd_results));
597 if (!results) {
598 sway_log(L_ERROR, "Unable to allocate command results");
599 return NULL;
600 }
587 results->status = status; 601 results->status = status;
588 if (input) { 602 if (input) {
589 results->input = strdup(input); // input is the command name 603 results->input = strdup(input); // input is the command name
@@ -594,7 +608,9 @@ struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, c
594 char *error = malloc(256); 608 char *error = malloc(256);
595 va_list args; 609 va_list args;
596 va_start(args, format); 610 va_start(args, format);
597 vsnprintf(error, 256, format, args); 611 if (error) {
612 vsnprintf(error, 256, format, args);
613 }
598 va_end(args); 614 va_end(args);
599 results->error = error; 615 results->error = error;
600 } else { 616 } else {
diff --git a/sway/commands/assign.c b/sway/commands/assign.c
index 53c599ca..992b4692 100644
--- a/sway/commands/assign.c
+++ b/sway/commands/assign.c
@@ -23,10 +23,16 @@ struct cmd_results *cmd_assign(int argc, char **argv) {
23 char *movecmd = "move container to workspace "; 23 char *movecmd = "move container to workspace ";
24 int arglen = strlen(movecmd) + strlen(*argv) + 1; 24 int arglen = strlen(movecmd) + strlen(*argv) + 1;
25 char *cmdlist = calloc(1, arglen); 25 char *cmdlist = calloc(1, arglen);
26 26 if (!cmdlist) {
27 return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate command list");
28 }
27 snprintf(cmdlist, arglen, "%s%s", movecmd, *argv); 29 snprintf(cmdlist, arglen, "%s%s", movecmd, *argv);
28 30
29 struct criteria *crit = malloc(sizeof(struct criteria)); 31 struct criteria *crit = malloc(sizeof(struct criteria));
32 if (!crit) {
33 free(cmdlist);
34 return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria");
35 }
30 crit->crit_raw = strdup(criteria); 36 crit->crit_raw = strdup(criteria);
31 crit->cmdlist = cmdlist; 37 crit->cmdlist = cmdlist;
32 crit->tokens = create_list(); 38 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..8b3b0aac 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..01ac9f4e 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -26,6 +26,9 @@ struct cmd_results *cmd_output(int argc, char **argv) {
26 const char *name = argv[0]; 26 const char *name = argv[0];
27 27
28 struct output_config *output = calloc(1, sizeof(struct output_config)); 28 struct output_config *output = calloc(1, sizeof(struct output_config));
29 if (!output) {
30 return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config");
31 }
29 output->x = output->y = output->width = output->height = -1; 32 output->x = output->y = output->width = output->height = -1;
30 output->name = strdup(name); 33 output->name = strdup(name);
31 output->enabled = -1; 34 output->enabled = -1;
@@ -113,12 +116,20 @@ struct cmd_results *cmd_output(int argc, char **argv) {
113 src = p.we_wordv[0]; 116 src = p.we_wordv[0];
114 if (config->reading && *src != '/') { 117 if (config->reading && *src != '/') {
115 char *conf = strdup(config->current_config); 118 char *conf = strdup(config->current_config);
116 char *conf_path = dirname(conf); 119 if (conf) {
117 src = malloc(strlen(conf_path) + strlen(src) + 2); 120 char *conf_path = dirname(conf);
118 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); 121 src = malloc(strlen(conf_path) + strlen(src) + 2);
119 free(conf); 122 if (src) {
123 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
124 } else {
125 sway_log(L_ERROR, "Unable to allocate background source");
126 }
127 free(conf);
128 } else {
129 sway_log(L_ERROR, "Unable to allocate background source");
130 }
120 } 131 }
121 if (access(src, F_OK) == -1) { 132 if (!src || access(src, F_OK) == -1) {
122 return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src); 133 return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
123 } 134 }
124 for (char *m = mode; *m; ++m) *m = tolower(*m); 135 for (char *m = mode; *m; ++m) *m = tolower(*m);
diff --git a/sway/commands/permit.c b/sway/commands/permit.c
index 7a25e4ce..dee246d7 100644
--- a/sway/commands/permit.c
+++ b/sway/commands/permit.c
@@ -50,6 +50,9 @@ static struct feature_policy *get_policy(const char *name) {
50 } 50 }
51 if (!policy) { 51 if (!policy) {
52 policy = alloc_feature_policy(name); 52 policy = alloc_feature_policy(name);
53 if (!policy) {
54 sway_abort("Unable to allocate security policy");
55 }
53 list_add(config->feature_policies, policy); 56 list_add(config->feature_policies, policy);
54 } 57 }
55 return policy; 58 return policy;
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);
diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c
index 35224f8a..14fe242f 100644
--- a/sway/commands/workspace.c
+++ b/sway/commands/workspace.c
@@ -61,6 +61,10 @@ struct cmd_results *cmd_workspace(int argc, char **argv) {
61 return error; 61 return error;
62 } 62 }
63 struct workspace_output *wso = calloc(1, sizeof(struct workspace_output)); 63 struct workspace_output *wso = calloc(1, sizeof(struct workspace_output));
64 if (!wso) {
65 return cmd_results_new(CMD_FAILURE, "workspace output",
66 "Unable to allocate workspace output");
67 }
64 wso->workspace = strdup(argv[0]); 68 wso->workspace = strdup(argv[0]);
65 wso->output = strdup(argv[2]); 69 wso->output = strdup(argv[2]);
66 int i = -1; 70 int i = -1;
diff --git a/sway/config.c b/sway/config.c
index e737f83c..4164cefa 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -29,21 +29,30 @@ struct sway_config *config = NULL;
29static void terminate_swaybar(pid_t pid); 29static void terminate_swaybar(pid_t pid);
30 30
31static void free_variable(struct sway_variable *var) { 31static void free_variable(struct sway_variable *var) {
32 if (!var) {
33 return;
34 }
32 free(var->name); 35 free(var->name);
33 free(var->value); 36 free(var->value);
34 free(var); 37 free(var);
35} 38}
36 39
37static void free_binding(struct sway_binding *bind) { 40static void free_binding(struct sway_binding *bind) {
41 if (!bind) {
42 return;
43 }
38 free_flat_list(bind->keys); 44 free_flat_list(bind->keys);
39 free(bind->command); 45 free(bind->command);
40 free(bind); 46 free(bind);
41} 47}
42 48
43static void free_mode(struct sway_mode *mode) { 49static void free_mode(struct sway_mode *mode) {
50 if (!mode) {
51 return;
52 }
44 free(mode->name); 53 free(mode->name);
45 int i; 54 int i;
46 for (i = 0; i < mode->bindings->length; ++i) { 55 for (i = 0; mode->bindings && i < mode->bindings->length; ++i) {
47 free_binding(mode->bindings->items[i]); 56 free_binding(mode->bindings->items[i]);
48 } 57 }
49 list_free(mode->bindings); 58 list_free(mode->bindings);
@@ -51,13 +60,16 @@ static void free_mode(struct sway_mode *mode) {
51} 60}
52 61
53static void free_bar(struct bar_config *bar) { 62static void free_bar(struct bar_config *bar) {
63 if (!bar) {
64 return;
65 }
54 free(bar->mode); 66 free(bar->mode);
55 free(bar->hidden_state); 67 free(bar->hidden_state);
56 free(bar->status_command); 68 free(bar->status_command);
57 free(bar->font); 69 free(bar->font);
58 free(bar->separator_symbol); 70 free(bar->separator_symbol);
59 int i; 71 int i;
60 for (i = 0; i < bar->bindings->length; ++i) { 72 for (i = 0; bar->bindings && i < bar->bindings->length; ++i) {
61 free_sway_mouse_binding(bar->bindings->items[i]); 73 free_sway_mouse_binding(bar->bindings->items[i]);
62 } 74 }
63 list_free(bar->bindings); 75 list_free(bar->bindings);
@@ -96,16 +108,25 @@ static void free_bar(struct bar_config *bar) {
96} 108}
97 109
98void free_input_config(struct input_config *ic) { 110void free_input_config(struct input_config *ic) {
111 if (!ic) {
112 return;
113 }
99 free(ic->identifier); 114 free(ic->identifier);
100 free(ic); 115 free(ic);
101} 116}
102 117
103void free_output_config(struct output_config *oc) { 118void free_output_config(struct output_config *oc) {
119 if (!oc) {
120 return;
121 }
104 free(oc->name); 122 free(oc->name);
105 free(oc); 123 free(oc);
106} 124}
107 125
108static void free_workspace_output(struct workspace_output *wo) { 126static void free_workspace_output(struct workspace_output *wo) {
127 if (!wo) {
128 return;
129 }
109 free(wo->output); 130 free(wo->output);
110 free(wo->workspace); 131 free(wo->workspace);
111 free(wo); 132 free(wo);
@@ -134,6 +155,10 @@ void pid_workspace_add(struct pid_workspace *pw) {
134 struct pid_workspace *list_pw = NULL; 155 struct pid_workspace *list_pw = NULL;
135 struct timespec ts; 156 struct timespec ts;
136 time_t *now = malloc(sizeof(time_t)); 157 time_t *now = malloc(sizeof(time_t));
158 if (!now) {
159 sway_log(L_ERROR, "Allocating time for pid_workspace failed");
160 return;
161 }
137 162
138 pid_workspace_cleanup(); 163 pid_workspace_cleanup();
139 164
@@ -168,65 +193,74 @@ void free_pid_workspace(struct pid_workspace *pw) {
168} 193}
169 194
170void free_command_policy(struct command_policy *policy) { 195void free_command_policy(struct command_policy *policy) {
196 if (!policy) {
197 return;
198 }
171 free(policy->command); 199 free(policy->command);
172 free(policy); 200 free(policy);
173} 201}
174 202
175void free_feature_policy(struct feature_policy *policy) { 203void free_feature_policy(struct feature_policy *policy) {
204 if (!policy) {
205 return;
206 }
176 free(policy->program); 207 free(policy->program);
177 free(policy); 208 free(policy);
178} 209}
179 210
180void free_config(struct sway_config *config) { 211void free_config(struct sway_config *config) {
212 if (!config) {
213 return;
214 }
181 int i; 215 int i;
182 for (i = 0; i < config->symbols->length; ++i) { 216 for (i = 0; config->symbols && i < config->symbols->length; ++i) {
183 free_variable(config->symbols->items[i]); 217 free_variable(config->symbols->items[i]);
184 } 218 }
185 list_free(config->symbols); 219 list_free(config->symbols);
186 220
187 for (i = 0; i < config->modes->length; ++i) { 221 for (i = 0; config->modes && i < config->modes->length; ++i) {
188 free_mode(config->modes->items[i]); 222 free_mode(config->modes->items[i]);
189 } 223 }
190 list_free(config->modes); 224 list_free(config->modes);
191 225
192 for (i = 0; i < config->bars->length; ++i) { 226 for (i = 0; config->bars && i < config->bars->length; ++i) {
193 free_bar(config->bars->items[i]); 227 free_bar(config->bars->items[i]);
194 } 228 }
195 list_free(config->bars); 229 list_free(config->bars);
196 230
197 free_flat_list(config->cmd_queue); 231 free_flat_list(config->cmd_queue);
198 232
199 for (i = 0; i < config->workspace_outputs->length; ++i) { 233 for (i = 0; config->workspace_outputs && i < config->workspace_outputs->length; ++i) {
200 free_workspace_output(config->workspace_outputs->items[i]); 234 free_workspace_output(config->workspace_outputs->items[i]);
201 } 235 }
202 list_free(config->workspace_outputs); 236 list_free(config->workspace_outputs);
203 237
204 for (i = 0; i < config->pid_workspaces->length; ++i) { 238 for (i = 0; config->pid_workspaces && i < config->pid_workspaces->length; ++i) {
205 free_pid_workspace(config->pid_workspaces->items[i]); 239 free_pid_workspace(config->pid_workspaces->items[i]);
206 } 240 }
207 list_free(config->pid_workspaces); 241 list_free(config->pid_workspaces);
208 242
209 for (i = 0; i < config->criteria->length; ++i) { 243 for (i = 0; config->criteria && i < config->criteria->length; ++i) {
210 free_criteria(config->criteria->items[i]); 244 free_criteria(config->criteria->items[i]);
211 } 245 }
212 list_free(config->criteria); 246 list_free(config->criteria);
213 247
214 for (i = 0; i < config->input_configs->length; ++i) { 248 for (i = 0; config->input_configs && i < config->input_configs->length; ++i) {
215 free_input_config(config->input_configs->items[i]); 249 free_input_config(config->input_configs->items[i]);
216 } 250 }
217 list_free(config->input_configs); 251 list_free(config->input_configs);
218 252
219 for (i = 0; i < config->output_configs->length; ++i) { 253 for (i = 0; config->output_configs && i < config->output_configs->length; ++i) {
220 free_output_config(config->output_configs->items[i]); 254 free_output_config(config->output_configs->items[i]);
221 } 255 }
222 list_free(config->output_configs); 256 list_free(config->output_configs);
223 257
224 for (i = 0; i < config->command_policies->length; ++i) { 258 for (i = 0; config->command_policies && i < config->command_policies->length; ++i) {
225 free_command_policy(config->command_policies->items[i]); 259 free_command_policy(config->command_policies->items[i]);
226 } 260 }
227 list_free(config->command_policies); 261 list_free(config->command_policies);
228 262
229 for (i = 0; i < config->feature_policies->length; ++i) { 263 for (i = 0; config->feature_policies && i < config->feature_policies->length; ++i) {
230 free_feature_policy(config->feature_policies->items[i]); 264 free_feature_policy(config->feature_policies->items[i]);
231 } 265 }
232 list_free(config->feature_policies); 266 list_free(config->feature_policies);
@@ -243,37 +277,37 @@ void free_config(struct sway_config *config) {
243 277
244 278
245static bool file_exists(const char *path) { 279static bool file_exists(const char *path) {
246 return access(path, R_OK) != -1; 280 return path && access(path, R_OK) != -1;
247} 281}
248 282
249static void config_defaults(struct sway_config *config) { 283static void config_defaults(struct sway_config *config) {
250 config->symbols = create_list(); 284 if (!(config->symbols = create_list())) goto cleanup;
251 config->modes = create_list(); 285 if (!(config->modes = create_list())) goto cleanup;
252 config->bars = create_list(); 286 if (!(config->bars = create_list())) goto cleanup;
253 config->workspace_outputs = create_list(); 287 if (!(config->workspace_outputs = create_list())) goto cleanup;
254 config->pid_workspaces = create_list(); 288 if (!(config->pid_workspaces = create_list())) goto cleanup;
255 config->criteria = create_list(); 289 if (!(config->criteria = create_list())) goto cleanup;
256 config->input_configs = create_list(); 290 if (!(config->input_configs = create_list())) goto cleanup;
257 config->output_configs = create_list(); 291 if (!(config->output_configs = create_list())) goto cleanup;
258 292
259 config->cmd_queue = create_list(); 293 if (!(config->cmd_queue = create_list())) goto cleanup;
260 294
261 config->current_mode = malloc(sizeof(struct sway_mode)); 295 if (!(config->current_mode = malloc(sizeof(struct sway_mode)))) goto cleanup;
262 config->current_mode->name = malloc(sizeof("default")); 296 if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup;
263 strcpy(config->current_mode->name, "default"); 297 strcpy(config->current_mode->name, "default");
264 config->current_mode->bindings = create_list(); 298 if (!(config->current_mode->bindings = create_list())) goto cleanup;
265 list_add(config->modes, config->current_mode); 299 list_add(config->modes, config->current_mode);
266 300
267 config->floating_mod = 0; 301 config->floating_mod = 0;
268 config->dragging_key = M_LEFT_CLICK; 302 config->dragging_key = M_LEFT_CLICK;
269 config->resizing_key = M_RIGHT_CLICK; 303 config->resizing_key = M_RIGHT_CLICK;
270 config->floating_scroll_up_cmd = strdup(""); 304 if (!(config->floating_scroll_up_cmd = strdup(""))) goto cleanup;
271 config->floating_scroll_down_cmd = strdup(""); 305 if (!(config->floating_scroll_down_cmd = strdup(""))) goto cleanup;
272 config->floating_scroll_left_cmd = strdup(""); 306 if (!(config->floating_scroll_left_cmd = strdup(""))) goto cleanup;
273 config->floating_scroll_right_cmd = strdup(""); 307 if (!(config->floating_scroll_right_cmd = strdup(""))) goto cleanup;
274 config->default_layout = L_NONE; 308 config->default_layout = L_NONE;
275 config->default_orientation = L_NONE; 309 config->default_orientation = L_NONE;
276 config->font = strdup("monospace 10"); 310 if (!(config->font = strdup("monospace 10"))) goto cleanup;
277 config->font_height = get_font_text_height(config->font); 311 config->font_height = get_font_text_height(config->font);
278 312
279 // floating view 313 // floating view
@@ -297,9 +331,9 @@ static void config_defaults(struct sway_config *config) {
297 config->gaps_inner = 0; 331 config->gaps_inner = 0;
298 config->gaps_outer = 0; 332 config->gaps_outer = 0;
299 333
300 config->active_bar_modifiers = create_list(); 334 if (!(config->active_bar_modifiers = create_list())) goto cleanup;
301 335
302 config->config_chain = create_list(); 336 if (!(config->config_chain = create_list())) goto cleanup;
303 config->current_config = NULL; 337 config->current_config = NULL;
304 338
305 // borders 339 // borders
@@ -343,9 +377,13 @@ static void config_defaults(struct sway_config *config) {
343 config->border_colors.background = 0xFFFFFFFF; 377 config->border_colors.background = 0xFFFFFFFF;
344 378
345 // Security 379 // Security
346 config->command_policies = create_list(); 380 if (!(config->command_policies = create_list())) goto cleanup;
347 config->feature_policies = create_list(); 381 if (!(config->feature_policies = create_list())) goto cleanup;
348 config->ipc_policy = UINT32_MAX; 382 config->ipc_policy = UINT32_MAX;
383
384 return;
385cleanup:
386 sway_abort("Unable to allocate config structures");
349} 387}
350 388
351static int compare_modifiers(const void *left, const void *right) { 389static int compare_modifiers(const void *left, const void *right) {
@@ -386,11 +424,15 @@ static char *get_config_path(void) {
386 if (!getenv("XDG_CONFIG_HOME")) { 424 if (!getenv("XDG_CONFIG_HOME")) {
387 char *home = getenv("HOME"); 425 char *home = getenv("HOME");
388 char *config_home = malloc(strlen(home) + strlen("/.config") + 1); 426 char *config_home = malloc(strlen(home) + strlen("/.config") + 1);
389 strcpy(config_home, home); 427 if (!config_home) {
390 strcat(config_home, "/.config"); 428 sway_log(L_ERROR, "Unable to allocate $HOME/.config");
391 setenv("XDG_CONFIG_HOME", config_home, 1); 429 } else {
392 sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); 430 strcpy(config_home, home);
393 free(config_home); 431 strcat(config_home, "/.config");
432 setenv("XDG_CONFIG_HOME", config_home, 1);
433 sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home);
434 free(config_home);
435 }
394 } 436 }
395 437
396 wordexp_t p; 438 wordexp_t p;
@@ -452,6 +494,9 @@ bool load_main_config(const char *file, bool is_active) {
452 494
453 struct sway_config *old_config = config; 495 struct sway_config *old_config = config;
454 config = calloc(1, sizeof(struct sway_config)); 496 config = calloc(1, sizeof(struct sway_config));
497 if (!config) {
498 sway_abort("Unable to allocate config");
499 }
455 500
456 config_defaults(config); 501 config_defaults(config);
457 if (is_active) { 502 if (is_active) {
@@ -491,6 +536,10 @@ static bool load_include_config(const char *path, const char *parent_dir, struct
491 if (len >= 1 && path[0] != '/') { 536 if (len >= 1 && path[0] != '/') {
492 len = len + strlen(parent_dir) + 2; 537 len = len + strlen(parent_dir) + 2;
493 full_path = malloc(len * sizeof(char)); 538 full_path = malloc(len * sizeof(char));
539 if (!full_path) {
540 sway_log(L_ERROR, "Unable to allocate full path to included config");
541 return false;
542 }
494 snprintf(full_path, len, "%s/%s", parent_dir, path); 543 snprintf(full_path, len, "%s/%s", parent_dir, path);
495 } 544 }
496 545
@@ -575,6 +624,9 @@ bool read_config(FILE *file, struct sway_config *config) {
575 char *line; 624 char *line;
576 while (!feof(file)) { 625 while (!feof(file)) {
577 line = read_line(file); 626 line = read_line(file);
627 if (!line) {
628 continue;
629 }
578 line_number++; 630 line_number++;
579 line = strip_whitespace(line); 631 line = strip_whitespace(line);
580 if (line[0] == '#') { 632 if (line[0] == '#') {
@@ -816,6 +868,10 @@ static void invoke_swaybar(struct bar_config *bar) {
816 // run custom swaybar 868 // run custom swaybar
817 int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5; 869 int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5;
818 char *command = malloc(len * sizeof(char)); 870 char *command = malloc(len * sizeof(char));
871 if (!command) {
872 sway_log(L_ERROR, "Unable to allocate swaybar command string");
873 return;
874 }
819 snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id); 875 snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id);
820 876
821 char *const cmd[] = { 877 char *const cmd[] = {
@@ -1052,6 +1108,11 @@ char *do_var_replacement(char *str) {
1052 if (strncmp(find, var->name, vnlen) == 0) { 1108 if (strncmp(find, var->name, vnlen) == 0) {
1053 int vvlen = strlen(var->value); 1109 int vvlen = strlen(var->value);
1054 char *newstr = malloc(strlen(str) - vnlen + vvlen + 1); 1110 char *newstr = malloc(strlen(str) - vnlen + vvlen + 1);
1111 if (!newstr) {
1112 sway_log(L_ERROR,
1113 "Unable to allocate replacement during variable expansion");
1114 break;
1115 }
1055 char *newptr = newstr; 1116 char *newptr = newstr;
1056 int offset = find - str; 1117 int offset = find - str;
1057 strncpy(newptr, str, offset); 1118 strncpy(newptr, str, offset);
@@ -1185,6 +1246,9 @@ void free_sway_mouse_binding(struct sway_mouse_binding *binding) {
1185 1246
1186struct sway_binding *sway_binding_dup(struct sway_binding *sb) { 1247struct sway_binding *sway_binding_dup(struct sway_binding *sb) {
1187 struct sway_binding *new_sb = malloc(sizeof(struct sway_binding)); 1248 struct sway_binding *new_sb = malloc(sizeof(struct sway_binding));
1249 if (!new_sb) {
1250 return NULL;
1251 }
1188 1252
1189 new_sb->order = sb->order; 1253 new_sb->order = sb->order;
1190 new_sb->modifiers = sb->modifiers; 1254 new_sb->modifiers = sb->modifiers;
@@ -1194,6 +1258,10 @@ struct sway_binding *sway_binding_dup(struct sway_binding *sb) {
1194 int i; 1258 int i;
1195 for (i = 0; i < sb->keys->length; ++i) { 1259 for (i = 0; i < sb->keys->length; ++i) {
1196 xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); 1260 xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
1261 if (!key) {
1262 free_sway_binding(new_sb);
1263 return NULL;
1264 }
1197 *key = *(xkb_keysym_t *)sb->keys->items[i]; 1265 *key = *(xkb_keysym_t *)sb->keys->items[i];
1198 list_add(new_sb->keys, key); 1266 list_add(new_sb->keys, key);
1199 } 1267 }
@@ -1204,13 +1272,16 @@ struct sway_binding *sway_binding_dup(struct sway_binding *sb) {
1204struct bar_config *default_bar_config(void) { 1272struct bar_config *default_bar_config(void) {
1205 struct bar_config *bar = NULL; 1273 struct bar_config *bar = NULL;
1206 bar = malloc(sizeof(struct bar_config)); 1274 bar = malloc(sizeof(struct bar_config));
1207 bar->mode = strdup("dock"); 1275 if (!bar) {
1208 bar->hidden_state = strdup("hide"); 1276 return NULL;
1277 }
1278 if (!(bar->mode = strdup("dock"))) goto cleanup;
1279 if (!(bar->hidden_state = strdup("hide"))) goto cleanup;
1209 bar->modifier = WLC_BIT_MOD_LOGO; 1280 bar->modifier = WLC_BIT_MOD_LOGO;
1210 bar->outputs = NULL; 1281 bar->outputs = NULL;
1211 bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; 1282 bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
1212 bar->bindings = create_list(); 1283 if (!(bar->bindings = create_list())) goto cleanup;
1213 bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p' && sleep 1; done"); 1284 if (!(bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p' && sleep 1; done"))) goto cleanup;
1214 bar->pango_markup = false; 1285 bar->pango_markup = false;
1215 bar->swaybar_command = NULL; 1286 bar->swaybar_command = NULL;
1216 bar->font = NULL; 1287 bar->font = NULL;
@@ -1224,21 +1295,21 @@ struct bar_config *default_bar_config(void) {
1224 bar->verbose = false; 1295 bar->verbose = false;
1225 bar->pid = 0; 1296 bar->pid = 0;
1226 // set default colors 1297 // set default colors
1227 bar->colors.background = strndup("#000000ff", 9); 1298 if (!(bar->colors.background = strndup("#000000ff", 9))) goto cleanup;
1228 bar->colors.statusline = strndup("#ffffffff", 9); 1299 if (!(bar->colors.statusline = strndup("#ffffffff", 9))) goto cleanup;
1229 bar->colors.separator = strndup("#666666ff", 9); 1300 if (!(bar->colors.separator = strndup("#666666ff", 9))) goto cleanup;
1230 bar->colors.focused_workspace_border = strndup("#4c7899ff", 9); 1301 if (!(bar->colors.focused_workspace_border = strndup("#4c7899ff", 9))) goto cleanup;
1231 bar->colors.focused_workspace_bg = strndup("#285577ff", 9); 1302 if (!(bar->colors.focused_workspace_bg = strndup("#285577ff", 9))) goto cleanup;
1232 bar->colors.focused_workspace_text = strndup("#ffffffff", 9); 1303 if (!(bar->colors.focused_workspace_text = strndup("#ffffffff", 9))) goto cleanup;
1233 bar->colors.active_workspace_border = strndup("#333333ff", 9); 1304 if (!(bar->colors.active_workspace_border = strndup("#333333ff", 9))) goto cleanup;
1234 bar->colors.active_workspace_bg = strndup("#5f676aff", 9); 1305 if (!(bar->colors.active_workspace_bg = strndup("#5f676aff", 9))) goto cleanup;
1235 bar->colors.active_workspace_text = strndup("#ffffffff", 9); 1306 if (!(bar->colors.active_workspace_text = strndup("#ffffffff", 9))) goto cleanup;
1236 bar->colors.inactive_workspace_border = strndup("#333333ff", 9); 1307 if (!(bar->colors.inactive_workspace_border = strndup("#333333ff", 9))) goto cleanup;
1237 bar->colors.inactive_workspace_bg = strndup("#222222ff", 9); 1308 if (!(bar->colors.inactive_workspace_bg = strndup("#222222ff", 9))) goto cleanup;
1238 bar->colors.inactive_workspace_text = strndup("#888888ff", 9); 1309 if (!(bar->colors.inactive_workspace_text = strndup("#888888ff", 9))) goto cleanup;
1239 bar->colors.urgent_workspace_border = strndup("#2f343aff", 9); 1310 if (!(bar->colors.urgent_workspace_border = strndup("#2f343aff", 9))) goto cleanup;
1240 bar->colors.urgent_workspace_bg = strndup("#900000ff", 9); 1311 if (!(bar->colors.urgent_workspace_bg = strndup("#900000ff", 9))) goto cleanup;
1241 bar->colors.urgent_workspace_text = strndup("#ffffffff", 9); 1312 if (!(bar->colors.urgent_workspace_text = strndup("#ffffffff", 9))) goto cleanup;
1242 // if the following colors stay undefined, they fall back to background, 1313 // if the following colors stay undefined, they fall back to background,
1243 // statusline, separator and urgent_workspace_*. 1314 // statusline, separator and urgent_workspace_*.
1244 bar->colors.focused_background = NULL; 1315 bar->colors.focused_background = NULL;
@@ -1251,4 +1322,8 @@ struct bar_config *default_bar_config(void) {
1251 list_add(config->bars, bar); 1322 list_add(config->bars, bar);
1252 1323
1253 return bar; 1324 return bar;
1325
1326cleanup:
1327 free_bar(bar);
1328 return NULL;
1254} 1329}
diff --git a/sway/container.c b/sway/container.c
index e5284200..d9677cdb 100644
--- a/sway/container.c
+++ b/sway/container.c
@@ -23,6 +23,9 @@ static swayc_t *new_swayc(enum swayc_types type) {
23 // next id starts at 1 because 0 is assigned to root_container in layout.c 23 // next id starts at 1 because 0 is assigned to root_container in layout.c
24 static size_t next_id = 1; 24 static size_t next_id = 1;
25 swayc_t *c = calloc(1, sizeof(swayc_t)); 25 swayc_t *c = calloc(1, sizeof(swayc_t));
26 if (!c) {
27 return NULL;
28 }
26 c->id = next_id++; 29 c->id = next_id++;
27 c->handle = -1; 30 c->handle = -1;
28 c->gaps = -1; 31 c->gaps = -1;
@@ -217,7 +220,7 @@ swayc_t *new_workspace(swayc_t *output, const char *name) {
217 workspace->y = output->y; 220 workspace->y = output->y;
218 workspace->width = output->width; 221 workspace->width = output->width;
219 workspace->height = output->height; 222 workspace->height = output->height;
220 workspace->name = strdup(name); 223 workspace->name = !name ? NULL : strdup(name);
221 workspace->visible = false; 224 workspace->visible = false;
222 workspace->floating = create_list(); 225 workspace->floating = create_list();
223 226
diff --git a/sway/extensions.c b/sway/extensions.c
index 96c7e60d..40702e28 100644
--- a/sway/extensions.c
+++ b/sway/extensions.c
@@ -23,6 +23,10 @@ static struct panel_config *find_or_create_panel_config(struct wl_resource *reso
23 } 23 }
24 sway_log(L_DEBUG, "Creating panel config for resource %p", resource); 24 sway_log(L_DEBUG, "Creating panel config for resource %p", resource);
25 struct panel_config *config = calloc(1, sizeof(struct panel_config)); 25 struct panel_config *config = calloc(1, sizeof(struct panel_config));
26 if (!config) {
27 sway_log(L_ERROR, "Unable to create panel config");
28 return NULL;
29 }
26 list_add(desktop_shell.panels, config); 30 list_add(desktop_shell.panels, config);
27 config->wl_resource = resource; 31 config->wl_resource = resource;
28 return config; 32 return config;
@@ -81,6 +85,10 @@ static void set_background(struct wl_client *client, struct wl_resource *resourc
81 } 85 }
82 sway_log(L_DEBUG, "Setting surface %p as background for output %d", surface, (int)output); 86 sway_log(L_DEBUG, "Setting surface %p as background for output %d", surface, (int)output);
83 struct background_config *config = malloc(sizeof(struct background_config)); 87 struct background_config *config = malloc(sizeof(struct background_config));
88 if (!config) {
89 sway_log(L_ERROR, "Unable to allocate background config");
90 return;
91 }
84 config->client = client; 92 config->client = client;
85 config->output = output; 93 config->output = output;
86 config->surface = wlc_resource_from_wl_surface_resource(surface); 94 config->surface = wlc_resource_from_wl_surface_resource(surface);
diff --git a/sway/handlers.c b/sway/handlers.c
index 86a976d8..3abe2fca 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -123,6 +123,11 @@ static void update_background_geometries(wlc_handle output) {
123 123
124static bool handle_input_created(struct libinput_device *device) { 124static bool handle_input_created(struct libinput_device *device) {
125 const char *identifier = libinput_dev_unique_id(device); 125 const char *identifier = libinput_dev_unique_id(device);
126 if (!identifier) {
127 sway_log(L_ERROR, "Unable to allocate unique name for input device %p",
128 device);
129 return true;
130 }
126 sway_log(L_INFO, "Found input device (%s)", identifier); 131 sway_log(L_INFO, "Found input device (%s)", identifier);
127 132
128 list_add(input_devices, device); 133 list_add(input_devices, device);
@@ -402,6 +407,10 @@ static bool handle_view_created(wlc_handle handle) {
402 } else { 407 } else {
403 swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT); 408 swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT);
404 wlc_handle *h = malloc(sizeof(wlc_handle)); 409 wlc_handle *h = malloc(sizeof(wlc_handle));
410 if (!h) {
411 sway_log(L_ERROR, "Unable to allocate window handle, view handler bailing out");
412 return true;
413 }
405 *h = handle; 414 *h = handle;
406 sway_log(L_DEBUG, "Adding unmanaged window %p to %p", h, output->unmanaged); 415 sway_log(L_DEBUG, "Adding unmanaged window %p to %p", h, output->unmanaged);
407 list_add(output->unmanaged, h); 416 list_add(output->unmanaged, h);
@@ -571,6 +580,10 @@ static void handle_binding_command(struct sway_binding *binding) {
571 // binding since it will be gone after the reload has completed. 580 // binding since it will be gone after the reload has completed.
572 if (strcasecmp(binding->command, "reload") == 0) { 581 if (strcasecmp(binding->command, "reload") == 0) {
573 binding_copy = sway_binding_dup(binding); 582 binding_copy = sway_binding_dup(binding);
583 if (!binding_copy) {
584 sway_log(L_ERROR, "Unable to duplicate binding during reload");
585 return;
586 }
574 reload = true; 587 reload = true;
575 } 588 }
576 589
diff --git a/sway/input.c b/sway/input.c
index acd69a6b..249d95c6 100644
--- a/sway/input.c
+++ b/sway/input.c
@@ -11,8 +11,16 @@
11 11
12struct input_config *new_input_config(const char* identifier) { 12struct input_config *new_input_config(const char* identifier) {
13 struct input_config *input = calloc(1, sizeof(struct input_config)); 13 struct input_config *input = calloc(1, sizeof(struct input_config));
14 if (!input) {
15 sway_log(L_DEBUG, "Unable to allocate input config");
16 return NULL;
17 }
14 sway_log(L_DEBUG, "new_input_config(%s)", identifier); 18 sway_log(L_DEBUG, "new_input_config(%s)", identifier);
15 input->identifier = strdup(identifier); 19 if (!(input->identifier = strdup(identifier))) {
20 free(input);
21 sway_log(L_DEBUG, "Unable to allocate input config");
22 return NULL;
23 }
16 24
17 input->tap = INT_MIN; 25 input->tap = INT_MIN;
18 input->drag_lock = INT_MIN; 26 input->drag_lock = INT_MIN;
@@ -45,6 +53,10 @@ char *libinput_dev_unique_id(struct libinput_device *device) {
45 53
46 int len = strlen(name) + sizeof(char) * 6; 54 int len = strlen(name) + sizeof(char) * 6;
47 char *identifier = malloc(len); 55 char *identifier = malloc(len);
56 if (!identifier) {
57 sway_log(L_ERROR, "Unable to allocate unique input device name");
58 return NULL;
59 }
48 60
49 const char *fmt = "%d:%d:%s"; 61 const char *fmt = "%d:%d:%s";
50 snprintf(identifier, len, fmt, vendor, product, name); 62 snprintf(identifier, len, fmt, vendor, product, name);
diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index e65e9de3..fd17216e 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -245,6 +245,15 @@ json_object *ipc_json_get_version() {
245 245
246#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE 246#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
247 char *full_version = calloc(strlen(SWAY_GIT_VERSION) + strlen(SWAY_GIT_BRANCH) + strlen(SWAY_VERSION_DATE) + 20, 1); 247 char *full_version = calloc(strlen(SWAY_GIT_VERSION) + strlen(SWAY_GIT_BRANCH) + strlen(SWAY_VERSION_DATE) + 20, 1);
248 if (!full_version) {
249 json_object_object_add(version, "human_readable",
250 json_object_new_string("Allocating version string failed"));
251 // TODO: it's stupid that we allocate this in the first place
252 json_object_object_add(version, "major", json_object_new_int(0));
253 json_object_object_add(version, "minor", json_object_new_int(0));
254 json_object_object_add(version, "patch", json_object_new_int(0));
255 return version;
256 }
248 strcat(full_version, SWAY_GIT_VERSION); 257 strcat(full_version, SWAY_GIT_VERSION);
249 strcat(full_version, " ("); 258 strcat(full_version, " (");
250 strcat(full_version, SWAY_VERSION_DATE); 259 strcat(full_version, SWAY_VERSION_DATE);
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index de72beca..be6e411a 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -106,7 +106,7 @@ void ipc_terminate(void) {
106struct sockaddr_un *ipc_user_sockaddr(void) { 106struct sockaddr_un *ipc_user_sockaddr(void) {
107 struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un)); 107 struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un));
108 if (ipc_sockaddr == NULL) { 108 if (ipc_sockaddr == NULL) {
109 sway_abort("can't malloc ipc_sockaddr"); 109 sway_abort("Can't allocate ipc_sockaddr");
110 } 110 }
111 111
112 ipc_sockaddr->sun_family = AF_UNIX; 112 ipc_sockaddr->sun_family = AF_UNIX;
@@ -119,7 +119,7 @@ struct sockaddr_un *ipc_user_sockaddr(void) {
119 } 119 }
120 if (path_size <= snprintf(ipc_sockaddr->sun_path, path_size, 120 if (path_size <= snprintf(ipc_sockaddr->sun_path, path_size,
121 "%s/sway-ipc.%i.%i.sock", dir, getuid(), getpid())) { 121 "%s/sway-ipc.%i.%i.sock", dir, getuid(), getpid())) {
122 sway_abort("socket path won't fit into ipc_sockaddr->sun_path"); 122 sway_abort("Socket path won't fit into ipc_sockaddr->sun_path");
123 } 123 }
124 124
125 return ipc_sockaddr; 125 return ipc_sockaddr;
@@ -148,13 +148,13 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
148 148
149 int client_fd = accept(ipc_socket, NULL, NULL); 149 int client_fd = accept(ipc_socket, NULL, NULL);
150 if (client_fd == -1) { 150 if (client_fd == -1) {
151 sway_log_errno(L_INFO, "Unable to accept IPC client connection"); 151 sway_log_errno(L_ERROR, "Unable to accept IPC client connection");
152 return 0; 152 return 0;
153 } 153 }
154 154
155 int flags; 155 int flags;
156 if ((flags=fcntl(client_fd, F_GETFD)) == -1 || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) { 156 if ((flags=fcntl(client_fd, F_GETFD)) == -1 || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
157 sway_log_errno(L_INFO, "Unable to set CLOEXEC on IPC client socket"); 157 sway_log_errno(L_ERROR, "Unable to set CLOEXEC on IPC client socket");
158 close(client_fd); 158 close(client_fd);
159 return 0; 159 return 0;
160 } 160 }
@@ -171,6 +171,11 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
171 } 171 }
172 172
173 struct ipc_client* client = malloc(sizeof(struct ipc_client)); 173 struct ipc_client* client = malloc(sizeof(struct ipc_client));
174 if (!client) {
175 sway_log(L_ERROR, "Unable to allocate ipc client");
176 close(client_fd);
177 return 0;
178 }
174 client->payload_length = 0; 179 client->payload_length = 0;
175 client->fd = client_fd; 180 client->fd = client_fd;
176 client->subscribed_events = 0; 181 client->subscribed_events = 0;
@@ -187,7 +192,7 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
187 struct ipc_client *client = data; 192 struct ipc_client *client = data;
188 193
189 if (mask & WLC_EVENT_ERROR) { 194 if (mask & WLC_EVENT_ERROR) {
190 sway_log(L_INFO, "IPC Client socket error, removing client"); 195 sway_log(L_ERROR, "IPC Client socket error, removing client");
191 client->fd = -1; 196 client->fd = -1;
192 ipc_client_disconnect(client); 197 ipc_client_disconnect(client);
193 return 0; 198 return 0;
@@ -291,6 +296,12 @@ void ipc_get_pixels(wlc_handle output) {
291 char response_header[9]; 296 char response_header[9];
292 memset(response_header, 0, sizeof(response_header)); 297 memset(response_header, 0, sizeof(response_header));
293 char *data = malloc(sizeof(response_header) + size->w * size->h * 4); 298 char *data = malloc(sizeof(response_header) + size->w * size->h * 4);
299 if (!data) {
300 sway_log(L_ERROR, "Unable to allocate pixels for get_pixels");
301 ipc_client_disconnect(req->client);
302 free(req);
303 continue;
304 }
294 wlc_pixels_read(WLC_RGBA8888, &req->geo, &g_out, data + sizeof(response_header)); 305 wlc_pixels_read(WLC_RGBA8888, &req->geo, &g_out, data + sizeof(response_header));
295 306
296 response_header[0] = 1; 307 response_header[0] = 1;
@@ -318,7 +329,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
318 329
319 char *buf = malloc(client->payload_length + 1); 330 char *buf = malloc(client->payload_length + 1);
320 if (!buf) { 331 if (!buf) {
321 sway_log_errno(L_INFO, "Out of memory"); 332 sway_log_errno(L_INFO, "Unable to allocate IPC payload");
322 ipc_client_disconnect(client); 333 ipc_client_disconnect(client);
323 return; 334 return;
324 } 335 }
@@ -414,7 +425,11 @@ void ipc_client_handle_command(struct ipc_client *client) {
414 struct libinput_device *device = input_devices->items[i]; 425 struct libinput_device *device = input_devices->items[i];
415 char* identifier = libinput_dev_unique_id(device); 426 char* identifier = libinput_dev_unique_id(device);
416 json_object *device_object = json_object_new_object(); 427 json_object *device_object = json_object_new_object();
417 json_object_object_add(device_object, "identifier", json_object_new_string(identifier)); 428 if (!identifier) {
429 json_object_object_add(device_object, "identifier", NULL);
430 } else {
431 json_object_object_add(device_object, "identifier", json_object_new_string(identifier));
432 }
418 json_object_array_add(inputs, device_object); 433 json_object_array_add(inputs, device_object);
419 free(identifier); 434 free(identifier);
420 } 435 }
@@ -493,6 +508,10 @@ void ipc_client_handle_command(struct ipc_client *client) {
493 goto exit_cleanup; 508 goto exit_cleanup;
494 } 509 }
495 struct get_pixels_request *req = malloc(sizeof(struct get_pixels_request)); 510 struct get_pixels_request *req = malloc(sizeof(struct get_pixels_request));
511 if (!req) {
512 sway_log(L_ERROR, "Unable to allocate get_pixels request");
513 goto exit_cleanup;
514 }
496 req->client = client; 515 req->client = client;
497 req->output = output->handle; 516 req->output = output->handle;
498 req->geo = g; 517 req->geo = g;
diff --git a/sway/main.c b/sway/main.c
index 157c61b3..d41eb292 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -53,7 +53,10 @@ void detect_proprietary() {
53 return; 53 return;
54 } 54 }
55 while (!feof(f)) { 55 while (!feof(f)) {
56 char *line = read_line(f); 56 char *line;
57 if (!(line = read_line(f))) {
58 break;
59 }
57 if (strstr(line, "nvidia")) { 60 if (strstr(line, "nvidia")) {
58 fprintf(stderr, "\x1B[1;31mWarning: Proprietary nvidia drivers do NOT support Wayland. Use nouveau.\x1B[0m\n"); 61 fprintf(stderr, "\x1B[1;31mWarning: Proprietary nvidia drivers do NOT support Wayland. Use nouveau.\x1B[0m\n");
59 fprintf(stderr, "\x1B[1;31mYes, they STILL don't work with the newly announced wayland \"support\".\x1B[0m\n"); 62 fprintf(stderr, "\x1B[1;31mYes, they STILL don't work with the newly announced wayland \"support\".\x1B[0m\n");
@@ -118,7 +121,10 @@ static void log_distro() {
118 if (f) { 121 if (f) {
119 sway_log(L_INFO, "Contents of %s:", paths[i]); 122 sway_log(L_INFO, "Contents of %s:", paths[i]);
120 while (!feof(f)) { 123 while (!feof(f)) {
121 char *line = read_line(f); 124 char *line;
125 if (!(line = read_line(f))) {
126 break;
127 }
122 if (*line) { 128 if (*line) {
123 sway_log(L_INFO, "%s", line); 129 sway_log(L_INFO, "%s", line);
124 } 130 }
@@ -136,7 +142,10 @@ static void log_kernel() {
136 return; 142 return;
137 } 143 }
138 while (!feof(f)) { 144 while (!feof(f)) {
139 char *line = read_line(f); 145 char *line;
146 if (!(line = read_line(f))) {
147 break;
148 }
140 if (*line) { 149 if (*line) {
141 sway_log(L_INFO, "%s", line); 150 sway_log(L_INFO, "%s", line);
142 } 151 }
diff --git a/sway/security.c b/sway/security.c
index 9cccd62e..41a3b94b 100644
--- a/sway/security.c
+++ b/sway/security.c
@@ -15,14 +15,28 @@ struct feature_policy *alloc_feature_policy(const char *program) {
15 } 15 }
16 16
17 struct feature_policy *policy = malloc(sizeof(struct feature_policy)); 17 struct feature_policy *policy = malloc(sizeof(struct feature_policy));
18 if (!policy) {
19 return NULL;
20 }
18 policy->program = strdup(program); 21 policy->program = strdup(program);
22 if (!policy->program) {
23 free(policy);
24 return NULL;
25 }
19 policy->features = default_policy; 26 policy->features = default_policy;
20 return policy; 27 return policy;
21} 28}
22 29
23struct command_policy *alloc_command_policy(const char *command) { 30struct command_policy *alloc_command_policy(const char *command) {
24 struct command_policy *policy = malloc(sizeof(struct command_policy)); 31 struct command_policy *policy = malloc(sizeof(struct command_policy));
32 if (!policy) {
33 return NULL;
34 }
25 policy->command = strdup(command); 35 policy->command = strdup(command);
36 if (!policy->command) {
37 free(policy);
38 return NULL;
39 }
26 policy->context = 0; 40 policy->context = 0;
27 return policy; 41 return policy;
28} 42}
@@ -35,12 +49,14 @@ enum secure_feature get_feature_policy(pid_t pid) {
35#endif 49#endif
36 int pathlen = snprintf(NULL, 0, fmt, pid); 50 int pathlen = snprintf(NULL, 0, fmt, pid);
37 char *path = malloc(pathlen + 1); 51 char *path = malloc(pathlen + 1);
38 snprintf(path, pathlen + 1, fmt, pid); 52 if (path) {
53 snprintf(path, pathlen + 1, fmt, pid);
54 }
39 static char link[2048]; 55 static char link[2048];
40 56
41 uint32_t default_policy = 0; 57 uint32_t default_policy = 0;
42 58
43 ssize_t len = readlink(path, link, sizeof(link)); 59 ssize_t len = !path ? -1 : readlink(path, link, sizeof(link));
44 if (len < 0) { 60 if (len < 0) {
45 sway_log(L_INFO, 61 sway_log(L_INFO,
46 "WARNING: unable to read %s for security check. Using default policy.", 62 "WARNING: unable to read %s for security check. Using default policy.",
diff --git a/sway/workspace.c b/sway/workspace.c
index 7b24d7d9..d804126b 100644
--- a/sway/workspace.c
+++ b/sway/workspace.c
@@ -121,6 +121,10 @@ char *workspace_next_name(const char *output_name) {
121 l = 3; 121 l = 3;
122 } 122 }
123 char *name = malloc(l + 1); 123 char *name = malloc(l + 1);
124 if (!name) {
125 sway_log(L_ERROR, "Could not allocate workspace name");
126 return NULL;
127 }
124 sprintf(name, "%d", ws_num++); 128 sprintf(name, "%d", ws_num++);
125 return name; 129 return name;
126} 130}
@@ -278,7 +282,11 @@ bool workspace_switch(swayc_t *workspace) {
278 || (strcmp(prev_workspace_name, active_ws->name) 282 || (strcmp(prev_workspace_name, active_ws->name)
279 && active_ws != workspace)) { 283 && active_ws != workspace)) {
280 free(prev_workspace_name); 284 free(prev_workspace_name);
281 prev_workspace_name = malloc(strlen(active_ws->name)+1); 285 prev_workspace_name = malloc(strlen(active_ws->name) + 1);
286 if (!prev_workspace_name) {
287 sway_log(L_ERROR, "Unable to allocate previous workspace name");
288 return false;
289 }
282 strcpy(prev_workspace_name, active_ws->name); 290 strcpy(prev_workspace_name, active_ws->name);
283 } 291 }
284 292
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index 8d2f4e9a..f2da7392 100644
--- a/swaybar/ipc.c
+++ b/swaybar/ipc.c
@@ -331,6 +331,9 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) {
331 331
332bool handle_ipc_event(struct bar *bar) { 332bool handle_ipc_event(struct bar *bar) {
333 struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd); 333 struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd);
334 if (!resp) {
335 return false;
336 }
334 switch (resp->type) { 337 switch (resp->type) {
335 case IPC_EVENT_WORKSPACE: 338 case IPC_EVENT_WORKSPACE:
336 ipc_update_workspaces(bar); 339 ipc_update_workspaces(bar);