Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
seamus: improve some low-hanging error handling/logging
src/queue.c | 5 +++++ src/seamus.c | 3 +-- src/status.c | 15 +++++++++++++++ src/ui.c | 10 ++++++++++
diff --git a/src/queue.c b/src/queue.c index 90e0a4966b8ce9cfd17488ebf33eb8a866138abc..1f68dbdf0d3639ddbef83f4544e30c500d96e45f 100644 --- a/src/queue.c +++ b/src/queue.c @@ -18,6 +18,11 @@ int index = 0; free(seamus->queue); seamus->queue = calloc(total, sizeof(struct seamus_song)); + if (seamus->queue == NULL) { + log_error("Failed to allocate memory for queue"); + seamus->queue_size = 0; + return 1; + } seamus->queue_size = total; while (index < total) { diff --git a/src/seamus.c b/src/seamus.c index 6bb75403e57a62435ddabdddfd14204168d625c3..886294980ab8ec58c00fc57a5a2db9fa667f28db 100755 --- a/src/seamus.c +++ b/src/seamus.c @@ -10,9 +10,8 @@ setup_connection(struct seamus_frontend *s) { struct mpd_connection *connection = mpd_connection_new(NULL, 0, 0); - // XXX: Check which conditions could lead to this being NULL and notify - // accordingly if (connection == NULL) { + log_error("Failed to create MPD connection (out of memory)"); return 1; } diff --git a/src/status.c b/src/status.c index 4dfdf40d699302b310c515d40e4b18efdb773678..a78057742f27e56ff6829f1d16e62c4d2343f117 100644 --- a/src/status.c +++ b/src/status.c @@ -21,6 +21,11 @@ } if (s->status == NULL) { s->status = calloc(1, sizeof(struct seamus_status)); + if (s->status == NULL) { + log_error("Failed to allocate memory for status"); + mpd_status_free(status); + return 1; + } } s->status->repeat = mpd_status_get_repeat(status); @@ -45,6 +50,12 @@ } } else { free(s->status->description); s->status->description = malloc(sizeof("Stopped")); + if (s->status->description == NULL) { + log_error("Failed to allocate memory for status description"); + mpd_status_free(status); + mpd_response_finish(s->conn); + return 1; + } sprintf(s->status->description, "%s", "Stopped"); } @@ -73,6 +84,10 @@ artist, title, em, es, tm, ts); free(status->description); status->description = malloc(n + 1); + if (status->description == NULL) { + log_error("Failed to allocate memory for description"); + return 1; + } sprintf(status->description, "%s - %s: %u:%02u/%u:%02u", artist, title, em, es, tm, ts); diff --git a/src/ui.c b/src/ui.c index 269c69570395366950d5e1a18fc117e1d0d9a889..26b29ebb0c898871729d4ee3cfb86cbc2314796d 100644 --- a/src/ui.c +++ b/src/ui.c @@ -44,10 +44,20 @@ .top = 2, .left = 2, .lines = tickit_window_lines(root) - 7, .cols = tickit_window_cols(root) - 2 }, 0); + if (!main_window) { + log_error("Failed to create main window"); + return 1; + } + TickitWindow *status_window = tickit_window_new(root, (TickitRect){ .top = tickit_window_lines(root) - 5, .left = 2, .lines = 5, .cols = tickit_window_cols(root) - 2 }, 0); + + if (!status_window) { + log_error("Failed to create status window"); + return 1; + } s->scrolling_pen = tickit_pen_new_attrs( TICKIT_PEN_BG, 3,