Skip to content

Commit b59a20c

Browse files
server UPDATE minor coverity fixes (#1727)
* test_url: extend compile switch to unneeded code * test_url: check return value of ftell * common: evaluate return value of curl_easy_setopt() * test_url: uncrustify fix --------- Co-authored-by: Martin Herberg <[email protected]>
1 parent 1afb8a0 commit b59a20c

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
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
}

tests/test_url.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "np2_test.h"
3333
#include "np2_test_config.h"
3434

35+
#ifdef NP2SRV_URL_FILE_PROTO
3536
static int
3637
setup_nacm_rules(void **state)
3738
{
@@ -240,6 +241,7 @@ test_copy_config_into_file(void **state)
240241
/* Get file size */
241242
fseek(file, 0, SEEK_END);
242243
size = ftell(file);
244+
assert_true(size > 0);
243245
rewind(file);
244246

245247
/* Allcate buffer */
@@ -331,6 +333,7 @@ test_copy_config_url2url(void **state)
331333
/* Get file size */
332334
fseek(file, 0, SEEK_END);
333335
size = ftell(file);
336+
assert_true(size > 0);
334337
rewind(file);
335338

336339
/* Allcate buffer */
@@ -427,9 +430,12 @@ test_edit_config(void **state)
427430
FREE_TEST_VARS(st);
428431
}
429432

433+
#endif /* NP2SRV_URL_FILE_PROTO */
434+
430435
int
431436
main(int argc, char **argv)
432437
{
438+
#ifdef NP2SRV_URL_FILE_PROTO
433439
const struct CMUnitTest tests[] = {
434440
cmocka_unit_test(test_validate),
435441
cmocka_unit_test_teardown(test_copy_config, teardown_data),
@@ -438,18 +444,21 @@ main(int argc, char **argv)
438444
cmocka_unit_test(test_copy_config_url2url),
439445
cmocka_unit_test_teardown(test_edit_config, teardown_data),
440446
};
447+
#endif
441448

442449
if (np2_is_nacm_recovery()) {
443450
puts("Running as NACM_RECOVERY_USER. Tests will not run correctly as this user bypases NACM. Skipping.");
444451
return 0;
445452
}
446453

447454
#ifndef NP2SRV_URL_FILE_PROTO
455+
(void) argc;
456+
(void) argv;
448457
puts("File protocol disabled, no tests to run. Skipping.");
449458
return 0;
450-
#endif
451-
459+
#else
452460
nc_verbosity(NC_VERB_WARNING);
453461
parse_arg(argc, argv);
454462
return cmocka_run_group_tests(tests, local_setup, local_teardown);
463+
#endif
455464
}

0 commit comments

Comments
 (0)