From d23a73f2b9ccf92ba18a60198c3280e37a0ee128 Mon Sep 17 00:00:00 2001 From: project-repo Date: Wed, 20 May 2020 11:53:46 +0200 Subject: [PATCH] Apply fixes suggested by -fanalyze --- cagebreak.c | 17 ++++++++++++++++- keybinding.c | 15 +++++++++++++-- message.c | 15 +++++++++++++++ output.c | 8 ++++++++ parse.c | 14 +++++++++++++- workspace.c | 26 ++++++++++++++++++++------ workspace.h | 4 ++-- 7 files changed, 87 insertions(+), 12 deletions(-) diff --git a/cagebreak.c b/cagebreak.c index 03cd5b9..28ba139 100644 --- a/cagebreak.c +++ b/cagebreak.c @@ -198,10 +198,12 @@ set_configuration(struct cg_server *server, if(parse_rc_line(server, line) != 0) { wlr_log(WLR_ERROR, "Error in config file \"%s\", line %d\n", config_file_path, line_num); + fclose(config_file); return -1; } } } + fclose(config_file); return 0; } @@ -218,6 +220,10 @@ get_config_file() { } char *config_path = malloc( (strlen(config_home_path) + strlen(addition) + 1) * sizeof(char)); + if(!config_path) { + wlr_log(WLR_ERROR, "Failed to allocate space for configuration path"); + return NULL; + } sprintf(config_path, "%s%s", config_home_path, addition); return config_path; } @@ -256,6 +262,12 @@ main(int argc, char *argv[]) { wlr_log_init(WLR_ERROR, NULL); #endif + server.modes = malloc(4 * sizeof(char *)); + if(!server.modes) { + wlr_log(WLR_ERROR,"Error allocating mode array"); + return -1; + } + /* Wayland requires XDG_RUNTIME_DIR to be set. */ if(!getenv("XDG_RUNTIME_DIR")) { wlr_log(WLR_ERROR, "XDG_RUNTIME_DIR is not set in the environment"); @@ -270,11 +282,14 @@ main(int argc, char *argv[]) { server.running = true; - server.modes = malloc(4 * sizeof(char *)); server.modes[0] = strdup("top"); server.modes[1] = strdup("root"); server.modes[2] = strdup("resize"); server.modes[3] = NULL; + if(!server.modes[0]||!server.modes[1]||server.modes[2]) { + wlr_log(WLR_ERROR,"Error allocating default modes"); + goto end; + } server.nws = 1; server.message_timeout = 2; diff --git a/keybinding.c b/keybinding.c index f6f5eaf..c9fe665 100644 --- a/keybinding.c +++ b/keybinding.c @@ -453,8 +453,10 @@ keybinding_workspace_fullscreen(struct cg_server *server) { } workspace_free_tiles(output->workspaces[output->curr_workspace]); - full_screen_workspace_tiles(output, - output->workspaces[output->curr_workspace]); + if(full_screen_workspace_tiles(server->output_layout,output->wlr_output, output->workspaces[output->curr_workspace])!=0) { + wlr_log(WLR_ERROR, "Failed to allocate space for fullscreen workspace"); + return; + } seat_set_focus(server->seat, current_view); } @@ -517,6 +519,10 @@ keybinding_split_output(struct cg_output *output, bool vertical) { } struct cg_tile *new_tile = calloc(1, sizeof(struct cg_tile)); + if(!new_tile) { + wlr_log(WLR_ERROR, "Failed to allocate new tile for splitting"); + return; + } new_tile->tile.x = new_x; new_tile->tile.y = new_y; new_tile->tile.width = x + width - new_x; @@ -741,6 +747,11 @@ keybinding_set_nws(struct cg_server *server, int nws) { output->workspaces = new_workspaces; for(int i = server->nws; i < nws; ++i) { output->workspaces[i] = full_screen_workspace(output); + if(!output->workspaces[i]) { + wlr_log(WLR_ERROR, "Failed to allocate additional workspaces"); + return; + } + wl_list_init(&output->workspaces[i]->views); wl_list_init(&output->workspaces[i]->unmanaged_views); } diff --git a/message.c b/message.c index 40ac033..bc24928 100644 --- a/message.c +++ b/message.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "cairo.h" #include "message.h" @@ -98,6 +99,11 @@ void message_set_output(struct cg_output *output, const char *string, struct wlr_box *box, enum cg_message_align align) { struct cg_message *message = malloc(sizeof(struct cg_message)); + if(!message) { + wlr_log(WLR_ERROR, "Error allocating message structure"); + free(box); + return; + } message->message = create_message_texture(string, output); message->position = box; wl_list_insert(&output->messages, &message->link); @@ -136,6 +142,10 @@ void message_printf(struct cg_output *output, const char *fmt, ...) { uint16_t buf_len = 256; char *buffer = (char *)malloc(buf_len * sizeof(char)); + if(buffer == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate buffer in message_printf"); + return; + } va_list ap; va_start(ap, fmt); @@ -143,6 +153,11 @@ message_printf(struct cg_output *output, const char *fmt, ...) { va_end(ap); struct wlr_box *box = malloc(sizeof(struct wlr_box)); + if(box == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate box in message_printf"); + free(buffer); + return; + } struct wlr_box *output_box = wlr_output_layout_get_box( output->server->output_layout, output->wlr_output); diff --git a/output.c b/output.c index 7983983..dbf75b5 100644 --- a/output.c +++ b/output.c @@ -580,6 +580,10 @@ handle_new_output(struct wl_listener *listener, void *data) { output->workspaces = malloc(server->nws * sizeof(struct cg_workspace *)); for(unsigned int i = 0; i < server->nws; ++i) { output->workspaces[i] = full_screen_workspace(output); + if(!output->workspaces[i]) { + wlr_log(WLR_ERROR, "Failed to allocate workspaces for output"); + return; + } wl_list_init(&output->workspaces[i]->views); wl_list_init(&output->workspaces[i]->unmanaged_views); } @@ -603,6 +607,10 @@ handle_new_output(struct wl_listener *listener, void *data) { for(unsigned int i = 0; i < server->nws; ++i) { workspace_free(output->workspaces[i]); output->workspaces[i] = full_screen_workspace(output); + if(!output->workspaces[i]) { + wlr_log(WLR_ERROR, "Failed to allocate workspaces for output"); + return; + } wl_list_init(&output->workspaces[i]->views); wl_list_init(&output->workspaces[i]->unmanaged_views); } diff --git a/parse.c b/parse.c index dae5164..33082c8 100644 --- a/parse.c +++ b/parse.c @@ -67,7 +67,11 @@ parse_command(struct cg_server *server, struct keybinding *keybinding, struct keybinding * parse_keybinding(struct cg_server *server, char **saveptr) { struct keybinding *keybinding = malloc(sizeof(struct keybinding)); - char *key = strtok_r(NULL, " ", saveptr); + if(keybinding == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate memory for keybinding in parse_keybinding"); + return NULL; + } +char *key = strtok_r(NULL, " ", saveptr); if(parse_key(keybinding, key) != 0) { wlr_log(WLR_ERROR, "Could not parse key definition \"%s\"", key); free(keybinding); @@ -153,6 +157,10 @@ parse_background(struct cg_server *server, float *color, char **saveptr) { struct keybinding * parse_escape(char **saveptr) { struct keybinding *keybinding = malloc(sizeof(struct keybinding)); + if(keybinding == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate memory for keybinding in parse_escape"); + return NULL; + } char *key = strtok_r(NULL, " ", saveptr); if(parse_key(keybinding, key) != 0) { wlr_log(WLR_ERROR, @@ -228,6 +236,10 @@ parse_float(char **saveptr, const char* delim) { int parse_output_config(struct wl_list *config_list, char **saveptr) { struct cg_output_config* cfg = malloc(sizeof(struct cg_output_config)); + if(cfg == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate memory for output configuration"); + goto error; + } char *name = strtok_r(NULL, " ", saveptr); if(name == NULL) { wlr_log(WLR_ERROR, "Expected name of output to be configured, got none"); diff --git a/workspace.c b/workspace.c index 8ed010e..10a476d 100644 --- a/workspace.c +++ b/workspace.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "message.h" #include "output.h" @@ -19,27 +20,36 @@ #include "view.h" #include "workspace.h" -void -full_screen_workspace_tiles(const struct cg_output *output, +int +full_screen_workspace_tiles(struct wlr_output_layout *layout,struct wlr_output *output, struct cg_workspace *workspace) { - workspace->server = output->server; workspace->focused_tile = calloc(1, sizeof(struct cg_tile)); + if(!workspace->focused_tile) { + return -1; + } workspace->focused_tile->workspace = workspace; workspace->focused_tile->next = workspace->focused_tile; workspace->focused_tile->prev = workspace->focused_tile; workspace->focused_tile->tile.x = 0; workspace->focused_tile->tile.y = 0; - struct wlr_box *output_box = wlr_output_layout_get_box( - output->server->output_layout, output->wlr_output); + struct wlr_box *output_box = wlr_output_layout_get_box(layout, output); workspace->focused_tile->tile.width = output_box->width; workspace->focused_tile->tile.height = output_box->height; workspace->focused_tile->view = NULL; + return 0; } struct cg_workspace * full_screen_workspace(struct cg_output *output) { struct cg_workspace *workspace = calloc(1, sizeof(struct cg_workspace)); - full_screen_workspace_tiles(output, workspace); + if(!workspace) { + return NULL; + } + workspace->server=output->server; + if(full_screen_workspace_tiles(output->server->output_layout, output->wlr_output, workspace)!=0) { + free(workspace); + return NULL; + } workspace->output = output; return workspace; } @@ -48,6 +58,10 @@ void workspace_focus_tile(struct cg_workspace *ws, struct cg_tile *tile) { ws->focused_tile = tile; struct wlr_box *box = malloc(sizeof(struct wlr_box)); + if(!box) { + wlr_log(WLR_ERROR, "Failed to allocate box required to focus tile"); + return; + } box->x = tile->tile.x + tile->tile.width / 2; box->y = tile->tile.y + tile->tile.height / 2; message_printf_pos(ws->output, box, CG_MESSAGE_CENTER, "Current frame"); diff --git a/workspace.h b/workspace.h index 069d036..34586f7 100644 --- a/workspace.h +++ b/workspace.h @@ -25,8 +25,8 @@ struct cg_workspace { struct cg_workspace * full_screen_workspace(struct cg_output *output); -void -full_screen_workspace_tiles(const struct cg_output *output, +int +full_screen_workspace_tiles(struct wlr_output_layout *layout,struct wlr_output *output, struct cg_workspace *workspace); void workspace_free_tiles(struct cg_workspace *workspace);