From ee259c617f8563aec4d59aabc4197fddb087d584 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sat, 3 Feb 2018 12:39:24 +0300 Subject: [PATCH] Fix some leaks in tr-remote reported by GCC's sanitizer --- daemon/remote.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/daemon/remote.c b/daemon/remote.c index cfe5810ea..605fd45e1 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -2055,13 +2055,28 @@ static CURL* tr_curl_easy_init(struct evbuffer* writebuf) { char* h = tr_strdup_printf("%s: %s", TR_RPC_SESSION_ID_HEADER, sessionId); struct curl_slist* custom_headers = curl_slist_append(NULL, h); + tr_free(h); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, custom_headers); - /* fixme: leaks */ + curl_easy_setopt(curl, CURLOPT_PRIVATE, custom_headers); } return curl; } +static void tr_curl_easy_cleanup(CURL* curl) +{ + struct curl_slist* custom_headers = NULL; + curl_easy_getinfo(curl, CURLINFO_PRIVATE, &custom_headers); + + curl_easy_cleanup(curl); + + if (custom_headers != NULL) + { + curl_slist_free_all(custom_headers); + } +} + static int flush(char const* rpcurl, tr_variant** benc) { CURLcode res; @@ -2101,7 +2116,7 @@ static int flush(char const* rpcurl, tr_variant** benc) /* Session id failed. Our curl header func has already * pulled the new session id from this response's headers, * build a new CURL* and try again */ - curl_easy_cleanup(curl); + tr_curl_easy_cleanup(curl); curl = NULL; status |= flush(rpcurl, benc); benc = NULL; @@ -2121,12 +2136,13 @@ static int flush(char const* rpcurl, tr_variant** benc) if (curl != NULL) { - curl_easy_cleanup(curl); + tr_curl_easy_cleanup(curl); } if (benc != NULL) { tr_variantFree(*benc); + tr_free(*benc); *benc = NULL; }