Skip to content

Commit d26f91f

Browse files
committed
common: evaluate return value of curl_easy_setopt()
1 parent 5542d30 commit d26f91f

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/common.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -819,11 +819,11 @@ url_readdata(void *ptr, size_t size, size_t nmemb, void *userdata)
819819
*
820820
* @param[in] curl CURL struct to modify.
821821
*/
822-
static void
822+
static CURLcode
823823
url_set_protocols(CURL *curl)
824824
{
825825
#if CURL_AT_LEAST_VERSION(7, 85, 0)
826-
curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, np2srv.url_protocols);
826+
return curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, np2srv.url_protocols);
827827
#else
828828
long proto = 0;
829829
char *ptr, *ptr2;
@@ -854,7 +854,7 @@ url_set_protocols(CURL *curl)
854854
ptr = ptr2 + 1;
855855
} while (ptr2[0]);
856856

857-
curl_easy_setopt(curl, CURLOPT_PROTOCOLS, proto);
857+
return curl_easy_setopt(curl, CURLOPT_PROTOCOLS, proto);
858858
#endif
859859
}
860860

@@ -871,7 +871,8 @@ url_get(const struct ly_ctx *ly_ctx, const char *url, char **url_data)
871871
{
872872
struct nc_server_reply *reply = NULL;
873873
CURL *curl;
874-
char curl_buffer[CURL_ERROR_SIZE];
874+
CURLcode res;
875+
char curl_buffer[CURL_ERROR_SIZE] = {0};
875876
struct np_url_mem mem_data = {0};
876877

877878
if (!np2srv.url_protocols) {
@@ -884,14 +885,26 @@ url_get(const struct ly_ctx *ly_ctx, const char *url, char **url_data)
884885
/* set up libcurl */
885886
curl_global_init(URL_INIT_FLAGS);
886887
curl = curl_easy_init();
887-
url_set_protocols(curl);
888-
curl_easy_setopt(curl, CURLOPT_URL, url);
889-
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, url_writedata);
890-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem_data);
891-
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_buffer);
892-
893-
/* download data */
894-
if (curl_easy_perform(curl) != CURLE_OK) {
888+
res = url_set_protocols(curl);
889+
if (res == CURLE_OK) {
890+
res = curl_easy_setopt(curl, CURLOPT_URL, url);
891+
}
892+
if (res == CURLE_OK) {
893+
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, url_writedata);
894+
}
895+
if (res == CURLE_OK) {
896+
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem_data);
897+
}
898+
if (res == CURLE_OK) {
899+
res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_buffer);
900+
}
901+
902+
if (res == CURLE_OK) {
903+
/* download data */
904+
res = curl_easy_perform(curl);
905+
}
906+
907+
if (res != CURLE_OK) {
895908
ERR("Failed to download data (curl: %s).", curl_buffer);
896909
reply = np_reply_err_op_failed(NULL, ly_ctx, curl_buffer);
897910
goto cleanup;
@@ -968,7 +981,7 @@ np_op_export_url(const struct ly_ctx *ly_ctx, const char *url, struct lyd_node *
968981
CURL *curl;
969982
struct np_url_mem mem_data;
970983
CURLcode r = 0;
971-
char curl_buffer[CURL_ERROR_SIZE], *str_data = NULL;
984+
char curl_buffer[CURL_ERROR_SIZE] = {0}, *str_data = NULL;
972985
struct lyd_node *config;
973986

974987
if (!np2srv.url_protocols) {
@@ -998,7 +1011,7 @@ np_op_export_url(const struct ly_ctx *ly_ctx, const char *url, struct lyd_node *
9981011
/* set up libcurl */
9991012
curl_global_init(URL_INIT_FLAGS);
10001013
curl = curl_easy_init();
1001-
url_set_protocols(curl);
1014+
r = url_set_protocols(curl);
10021015
if (!r) {
10031016
r = curl_easy_setopt(curl, CURLOPT_URL, url);
10041017
}

0 commit comments

Comments
 (0)