From 81e2129dd5a638ddc99194a307a1837eca96055f Mon Sep 17 00:00:00 2001 From: qorg11 Date: Sat, 9 Jul 2022 09:40:15 +0200 Subject: [PATCH] Moved the curl_mime thing inside the HTTP upload process, so there isn't a memory leak and the thing isn't allocated if it isn't needed. --- sakisafecli/sakisafecli.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/sakisafecli/sakisafecli.c b/sakisafecli/sakisafecli.c index f3d1165..dd7c083 100644 --- a/sakisafecli/sakisafecli.c +++ b/sakisafecli/sakisafecli.c @@ -69,22 +69,16 @@ main(int argc, char **argv) /* libcurl initialization */ CURL *easy_handle = curl_easy_init(); - curl_mime *mime; - mime = curl_mime_init(easy_handle); if(!easy_handle) { fprintf(stderr, "Error initializing libcurl\n"); return -1; } - if(!mime) { - fprintf(stderr, "Error initializing curl_mime\n"); - } if(argc == optind) { print_usage(); free(buffer); curl_easy_cleanup(easy_handle); - curl_mime_free(mime); return -1; } @@ -200,7 +194,12 @@ main(int argc, char **argv) curl_easy_setopt( easy_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER); - /* Form parameters */ + /* Common options for both HTTP and SCP transfers */ + + curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag); + curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress); + + /* File name */ @@ -211,6 +210,13 @@ main(int argc, char **argv) /* Process HTTP uploads */ if(protocol == CURLPROTO_HTTP || protocol == CURLPROTO_HTTPS) { + curl_mime *mime; + mime = curl_mime_init(easy_handle); + curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime); + if(!mime) { + fprintf(stderr, "Error initializing curl_mime\n"); + } + curl_mimepart *file_data; file_data = curl_mime_addpart(mime); char *filename = argv[optind]; @@ -223,10 +229,6 @@ main(int argc, char **argv) if(paste_flag) curl_mime_filename(file_data, "-"); - curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag); - curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress); - - curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime); curl_easy_perform(easy_handle); if(!silent_flag) putchar('\n'); @@ -244,8 +246,6 @@ main(int argc, char **argv) stat(argv[optind], &st); snprintf(path, 256, "%s/%s", server, filename); curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp); - curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag); - curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress); curl_easy_setopt( easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size); curl_easy_setopt(easy_handle, CURLOPT_URL, path);