From 825236c8bbcb1cfdd38f777ee317d67fa8927f6c Mon Sep 17 00:00:00 2001 From: chme Date: Fri, 20 Jan 2017 16:31:33 +0100 Subject: [PATCH] [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; };