From 825236c8bbcb1cfdd38f777ee317d67fa8927f6c Mon Sep 17 00:00:00 2001 From: chme Date: Fri, 20 Jan 2017 16:31:33 +0100 Subject: [PATCH 1/4] [spotify_webapi] Fix memory leaks --- src/spotify_webapi.c | 19 +++++++++++-------- src/spotify_webapi.h | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/spotify_webapi.c b/src/spotify_webapi.c index 8a9b075d..525f0cfb 100644 --- a/src/spotify_webapi.c +++ b/src/spotify_webapi.c @@ -136,7 +136,7 @@ jparse_str_from_array(json_object *array, int index, const char *key) } static void -http_client_ctx_free(struct http_client_ctx *ctx) +free_http_client_ctx(struct http_client_ctx *ctx) { if (!ctx) return; @@ -240,13 +240,19 @@ tokens_get(struct keyval *kv, const char **err) goto out_free_input_body; } + free(spotify_access_token); + spotify_access_token = NULL; + tmp = jparse_str_from_obj(haystack, "access_token"); if (tmp) - spotify_access_token = strdup(tmp); + spotify_access_token = strdup(tmp); tmp = jparse_str_from_obj(haystack, "refresh_token"); if (tmp) - spotify_refresh_token = strdup(tmp); + { + free(spotify_refresh_token); + spotify_refresh_token = strdup(tmp); + } expires_in = jparse_int_from_obj(haystack, "expires_in"); if (expires_in == 0) @@ -408,7 +414,7 @@ request_uri(struct spotify_request *request, const char *uri) void spotifywebapi_request_end(struct spotify_request *request) { - http_client_ctx_free(request->ctx); + free_http_client_ctx(request->ctx); jparse_free(request->haystack); } @@ -416,7 +422,6 @@ int spotifywebapi_request_next(struct spotify_request *request, const char *uri) { char *next_uri; - const char *tmp; int ret; if (request->ctx && !request->next_uri) @@ -444,9 +449,7 @@ spotifywebapi_request_next(struct spotify_request *request, const char *uri) return ret; request->total = jparse_int_from_obj(request->haystack, "total"); - tmp = jparse_str_from_obj(request->haystack, "next"); - if (tmp) - request->next_uri = strdup(tmp); + request->next_uri = jparse_str_from_obj(request->haystack, "next"); if (jparse_array_from_obj(request->haystack, "items", &request->items) < 0) { diff --git a/src/spotify_webapi.h b/src/spotify_webapi.h index af9efaf4..744724bc 100644 --- a/src/spotify_webapi.h +++ b/src/spotify_webapi.h @@ -91,7 +91,7 @@ struct spotify_request json_object *items; int count; int total; - char *next_uri; + const char *next_uri; int index; }; From dea5c50b67cbfd40d35e69c265ce2c9480ace213 Mon Sep 17 00:00:00 2001 From: chme Date: Fri, 20 Jan 2017 16:46:26 +0100 Subject: [PATCH 2/4] [player] Fix memleaks found with scan-build --- src/player.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/player.c b/src/player.c index 23bbd789..32225ca2 100644 --- a/src/player.c +++ b/src/player.c @@ -1388,7 +1388,10 @@ source_read(uint8_t *buf, int len, uint64_t rtptime) { ret = source_open(ps, cur_streaming->end + 1, 0); if (ret < 0) - return -1; + { + source_free(ps); + return -1; + } ret = source_play(); if (ret < 0) @@ -2485,6 +2488,7 @@ playback_prev_bh(void *arg, int *retval) ret = source_open(ps, last_rtptime + AIRTUNES_V2_PACKET_SAMPLES, 0); if (ret < 0) { + source_free(ps); playback_abort(); *retval = -1; @@ -2556,6 +2560,7 @@ playback_next_bh(void *arg, int *retval) ret = source_open(ps, last_rtptime + AIRTUNES_V2_PACKET_SAMPLES, 0); if (ret < 0) { + source_free(ps); playback_abort(); *retval = -1; return COMMAND_END; From 4e869f6fda288e53808b59a2edf87acf6e1c19af Mon Sep 17 00:00:00 2001 From: chme Date: Fri, 20 Jan 2017 16:50:09 +0100 Subject: [PATCH 3/4] [dacp] Fix indentation --- src/httpd_dacp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/httpd_dacp.c b/src/httpd_dacp.c index 56a5430f..f290e5f3 100644 --- a/src/httpd_dacp.c +++ b/src/httpd_dacp.c @@ -1074,7 +1074,7 @@ dacp_reply_cue_play(struct evhttp_request *req, struct evbuffer *evbuf, char **u dmap_send_error(req, "cacr", "Playback failed to start"); return; - } + } } else { @@ -1097,9 +1097,9 @@ dacp_reply_cue_play(struct evhttp_request *req, struct evbuffer *evbuf, char **u dmap_send_error(req, "cacr", "Playback failed to start"); return; + } } } - } ret = player_playback_start_byitem(queue_item); free_queue_item(queue_item, 0); From 6163269832efe9df2e822ffcc03a102fad83dc89 Mon Sep 17 00:00:00 2001 From: chme Date: Fri, 20 Jan 2017 17:03:45 +0100 Subject: [PATCH 4/4] [dacp/db] Fix issue found with scan-build - Result of operation is garbage or undefined in dacp_reply_cue_play - Uninitialized argument value in dacp_reply_playqueuecontents - Uninitialized argument value in queue_fetch_byposrelativetoitem --- src/db.c | 5 ++++- src/httpd_dacp.c | 13 ++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/db.c b/src/db.c index 7d8e8eae..ae2c2ca5 100644 --- a/src/db.c +++ b/src/db.c @@ -4691,7 +4691,10 @@ queue_fetch_byposrelativetoitem(int pos, uint32_t item_id, char shuffle, struct ret = queue_fetch_bypos(pos_absolute, shuffle, queue_item, with_metadata); - DPRINTF(E_DBG, L_DB, "Fetch by pos: fetched item (id=%d, pos=%d, file-id=%d)\n", queue_item->id, queue_item->pos, queue_item->file_id); + if (ret < 0) + DPRINTF(E_LOG, L_DB, "Error fetching item by pos: pos (%d) relative to item with id (%d)\n", pos, item_id); + else + DPRINTF(E_DBG, L_DB, "Fetch by pos: fetched item (id=%d, pos=%d, file-id=%d)\n", queue_item->id, queue_item->pos, queue_item->file_id); return ret; } diff --git a/src/httpd_dacp.c b/src/httpd_dacp.c index f290e5f3..b3bad157 100644 --- a/src/httpd_dacp.c +++ b/src/httpd_dacp.c @@ -1017,6 +1017,8 @@ dacp_reply_cue_play(struct evhttp_request *req, struct evbuffer *evbuf, char **u } } + player_get_status(&status); + cuequery = evhttp_find_header(query, "query"); if (cuequery) { @@ -1031,12 +1033,9 @@ dacp_reply_cue_play(struct evhttp_request *req, struct evbuffer *evbuf, char **u return; } } - else + else if (status.status != PLAY_STOPPED) { - player_get_status(&status); - - if (status.status != PLAY_STOPPED) - player_playback_stop(); + player_playback_stop(); } param = evhttp_find_header(query, "dacp.shufflestate"); @@ -1597,6 +1596,8 @@ dacp_reply_playqueuecontents(struct evhttp_request *req, struct evbuffer *evbuf, return; } + player_get_status(&status); + /* * If the span parameter is negativ make song list for Previously Played, * otherwise make song list for Up Next and begin with first song after playlist position. @@ -1626,8 +1627,6 @@ dacp_reply_playqueuecontents(struct evhttp_request *req, struct evbuffer *evbuf, } else { - player_get_status(&status); - memset(&query_params, 0, sizeof(struct query_params)); if (status.shuffle) query_params.sort = S_SHUFFLE_POS;