From cd4386228d9f72b436325aae798fea70aca9d3b6 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Thu, 20 Jan 2022 20:08:18 +0100 Subject: [PATCH] [spotify] Coverity fixups --- src/inputs/librespot-c/src/channel.c | 12 +++++++++--- src/inputs/librespot-c/src/commands.c | 7 +++++++ src/inputs/librespot-c/src/connection.c | 2 +- src/inputs/librespot-c/src/librespot-c.c | 3 +++ src/library/spotify_webapi.c | 11 +++++++---- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/inputs/librespot-c/src/channel.c b/src/inputs/librespot-c/src/channel.c index b3539840..fb4255e5 100644 --- a/src/inputs/librespot-c/src/channel.c +++ b/src/inputs/librespot-c/src/channel.c @@ -51,7 +51,7 @@ path_to_media_id_and_type(struct sp_file *file) struct sp_channel * channel_get(uint32_t channel_id, struct sp_session *session) { - if (channel_id > sizeof(session->channels)/sizeof(session->channels)[0]) + if (channel_id >= sizeof(session->channels)/sizeof(session->channels)[0]) return NULL; if (session->channels[channel_id].state == SP_CHANNEL_STATE_UNALLOCATED) @@ -148,6 +148,7 @@ channel_flush(struct sp_channel *channel) int fd = channel->audio_fd[0]; int flags; int got; + int ret; evbuffer_drain(channel->audio_buf, -1); @@ -157,13 +158,18 @@ channel_flush(struct sp_channel *channel) if (flags == -1) return -1; - fcntl(fd, F_SETFL, flags | O_NONBLOCK); + ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); + if (ret < 0) + return -1; do got = read(fd, buf, sizeof(buf)); while (got > 0); - fcntl(fd, F_SETFL, flags); + ret = fcntl(fd, F_SETFL, flags); + if (ret < 0) + return -1; + return 0; } diff --git a/src/inputs/librespot-c/src/commands.c b/src/inputs/librespot-c/src/commands.c index 09d66205..1b76e8e4 100644 --- a/src/inputs/librespot-c/src/commands.c +++ b/src/inputs/librespot-c/src/commands.c @@ -207,6 +207,10 @@ commands_base_new(struct event_base *evbase, command_exit_cb exit_cb) int ret; cmdbase = calloc(1, sizeof(struct commands_base)); + if (!cmdbase) + { + return NULL; + } #ifdef HAVE_PIPE2 ret = pipe2(cmdbase->command_pipe, O_CLOEXEC); @@ -370,6 +374,9 @@ commands_exec_async(struct commands_base *cmdbase, command_function func, void * int ret; cmd = calloc(1, sizeof(struct command)); + if (!cmd) + return -1; + cmd->func = func; cmd->func_bh = NULL; cmd->arg = arg; diff --git a/src/inputs/librespot-c/src/connection.c b/src/inputs/librespot-c/src/connection.c index abb6370d..7376f941 100644 --- a/src/inputs/librespot-c/src/connection.c +++ b/src/inputs/librespot-c/src/connection.c @@ -620,7 +620,7 @@ response_aplogin_failed(uint8_t *payload, size_t payload_len, struct sp_session } sp_errmsg = "(unknown login error)"; - for (int i = 0; i < sizeof(sp_login_errors); i++) + for (int i = 0; i < sizeof(sp_login_errors)/sizeof(sp_login_errors[0]); i++) { if (sp_login_errors[i].errorcode != aplogin_failed->error_code) continue; diff --git a/src/inputs/librespot-c/src/librespot-c.c b/src/inputs/librespot-c/src/librespot-c.c index f5ba80e1..3319b6a9 100644 --- a/src/inputs/librespot-c/src/librespot-c.c +++ b/src/inputs/librespot-c/src/librespot-c.c @@ -48,6 +48,7 @@ events for proceeding are activated directly. */ #include +#include #include "librespot-c-internal.h" #include "commands.h" @@ -833,6 +834,8 @@ librespotc_write(int fd, sp_progress_cb progress_cb, void *cb_arg) cmdargs = calloc(1, sizeof(struct sp_cmdargs)); + assert(cmdargs); + cmdargs->fd_read = fd; cmdargs->progress_cb = progress_cb; cmdargs->cb_arg = cb_arg; diff --git a/src/library/spotify_webapi.c b/src/library/spotify_webapi.c index 2df6db1f..cdeaf3c5 100644 --- a/src/library/spotify_webapi.c +++ b/src/library/spotify_webapi.c @@ -351,9 +351,10 @@ request_endpoint(const char *uri) json_object *json_response = NULL; int ret; - ctx = calloc(1, sizeof(struct http_client_ctx)); - ctx->output_headers = calloc(1, sizeof(struct keyval)); - ctx->input_body = evbuffer_new(); + CHECK_NULL(L_SPOTIFY, ctx = calloc(1, sizeof(struct http_client_ctx))); + CHECK_NULL(L_SPOTIFY, ctx->output_headers = calloc(1, sizeof(struct keyval))); + CHECK_NULL(L_SPOTIFY, ctx->input_body = evbuffer_new()); + ctx->url = uri; snprintf(bearer_token, sizeof(bearer_token), "Bearer %s", spotify_credentials.access_token); @@ -1091,7 +1092,9 @@ spotifywebapi_oauth_uri_get(const char *redirect_uri) if (param) { uri_len = strlen(spotify_auth_uri) + strlen(param) + 3; - uri = calloc(uri_len, sizeof(char)); + + CHECK_NULL(L_SPOTIFY, uri = calloc(uri_len, sizeof(char))); + snprintf(uri, uri_len, "%s/?%s", spotify_auth_uri, param); free(param);