From f17be6fdfb667818d6936972ec309fcae25a838c Mon Sep 17 00:00:00 2001 From: Tom Keddie <84747828+Tom-Keddie@users.noreply.github.com> Date: Tue, 21 Dec 2021 08:19:22 -0800 Subject: [PATCH] unum: misc code cleanup from coverity #3 (#51) MIN-10478 --- src/unum/speedtest/speedtest.c | 6 +--- src/unum/util/dns.c | 6 ++-- src/unum/util/util.c | 20 ++++++------ src/unum/util/util_json.c | 7 ++--- .../wireless/nl80211_platform/scan_platform.c | 7 +++-- src/unum/wireless/wireless.c | 8 ----- src/unum/zip/ioapi.c | 31 +++++++++++-------- 7 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/unum/speedtest/speedtest.c b/src/unum/speedtest/speedtest.c index e238337ff..4cd21f670 100644 --- a/src/unum/speedtest/speedtest.c +++ b/src/unum/speedtest/speedtest.c @@ -619,10 +619,6 @@ static int resolve_server_addr() endpoint = &settings.endpoints[0]; } - if(endpoint->domain == NULL) { - return -1; - } - if(util_get_ip4_addr(endpoint->domain, &s_addr) < 0 || s_addr.sa_family != AF_INET) { @@ -993,7 +989,7 @@ static int get_conn_time(struct sockaddr *sa, short port) if(so_error == 0) { retval = util_time(1000) - before; } else { - log("%s: Error: Could not connect to server! %d\n", __func__, strerror(so_error)); + log("%s: Error: Could not connect to server! %s\n", __func__, strerror(so_error)); } } else { diff --git a/src/unum/util/dns.c b/src/unum/util/dns.c index fd9240999..2739f4acd 100644 --- a/src/unum/util/dns.c +++ b/src/unum/util/dns.c @@ -535,7 +535,7 @@ static inline unsigned dns_k_permutor_powof(unsigned n) { unsigned m, i = 0; for (m = 1; m < n; m <<= 1, i++) - ;; + ; return i; } /* dns_k_permutor_powof() */ @@ -1008,7 +1008,7 @@ size_t dns_strlcpy(char *dst, const char *src, size_t lim) { } while (*s++ != '\0') - ;; + ; return s - src - 1; } /* dns_strlcpy() */ @@ -1032,7 +1032,7 @@ size_t dns_strlcat(char *dst, const char *src, size_t lim) { p = s; while (*s++ != '\0') - ;; + ; return lim + (s - p - 1); } /* dns_strlcat() */ diff --git a/src/unum/util/util.c b/src/unum/util/util.c index 47c458e7b..72ba607e3 100644 --- a/src/unum/util/util.c +++ b/src/unum/util/util.c @@ -221,7 +221,7 @@ int util_buf_to_file(char *file, void *buf, int len, mode_t crmode) // buf_size was not large enough to read all size_t util_file_to_buf(char *file, char *buf, size_t buf_size) { - size_t len = 0; + int len = 0; int fd = open(file, O_RDONLY); if(fd < 0) { return -1; @@ -259,10 +259,6 @@ int util_cmp_files_match(char *file1, char *file2, int must_exist) file1_len = file2_len = 0; - // open files - FILE *fd1 = fopen(file1, "r"); - FILE *fd2 = fopen(file2, "r"); - if(stat(file1, &st1) == 0) { file1_len = st1.st_size; } else if(must_exist || errno != ENOENT) { @@ -283,6 +279,9 @@ int util_cmp_files_match(char *file1, char *file2, int must_exist) do{ int status = 0; + // open files + FILE *fd1 = fopen(file1, "r"); + FILE *fd2 = fopen(file2, "r"); if (fd1 != NULL && fd2 != NULL) { @@ -294,18 +293,21 @@ int util_cmp_files_match(char *file1, char *file2, int must_exist) buf1[++len1] = '\0'; buf2[++len2] = '\0'; - // close files - fclose(fd1); - fclose(fd2); - // compare buffers status = memcmp(buf1, buf2, 512); if(status == 0) { + // close files + fclose(fd1); + fclose(fd2); + return TRUE; } } + // close files + if (fd1) fclose(fd1); + if (fd2) fclose(fd2); return FALSE; } while(0); diff --git a/src/unum/util/util_json.c b/src/unum/util/util_json.c index 17ea25fbd..8ce269a49 100644 --- a/src/unum/util/util_json.c +++ b/src/unum/util/util_json.c @@ -28,7 +28,7 @@ static json_t *util_tpl_to_json_val(JSON_VAL_TPL_t *val, char *key, int *perr) case JSON_VAL_STR: if(val->s && !(jval = json_string(val->s))) { log("%s: error adding '%s' value '%.*s%s'\n", __func__, - key, val->s, MAX_JSON_VAL_LOG, + key, MAX_JSON_VAL_LOG, val->s, (strlen(val->s) > MAX_JSON_VAL_LOG ? "..." : "")); *perr = -1; } @@ -84,13 +84,13 @@ static json_t *util_tpl_to_json_val(JSON_VAL_TPL_t *val, char *key, int *perr) } break; case JSON_VAL_FSTR: - if(val->fs != NULL) { + if(val->fs == NULL) { break; } s = val->fs(key); if(s && !(jval = json_string(s))) { log("%s: error adding '%s' value '%.*s%s'\n", __func__, - key, s, MAX_JSON_VAL_LOG, + key, MAX_JSON_VAL_LOG, s, (strlen(s) > MAX_JSON_VAL_LOG ? "..." : "")); *perr = -1; } @@ -247,7 +247,6 @@ json_t *util_tpl_to_json_obj(JSON_OBJ_TPL_t tpl) // Loop through all keyvalue pairs of the object for(kv = tpl; kv && kv->key; kv++) { - int err = 0; char *key = kv->key; JSON_VAL_TPL_t *val = &kv->val; diff --git a/src/unum/wireless/nl80211_platform/scan_platform.c b/src/unum/wireless/nl80211_platform/scan_platform.c index 857d0f8d8..2de25b51d 100644 --- a/src/unum/wireless/nl80211_platform/scan_platform.c +++ b/src/unum/wireless/nl80211_platform/scan_platform.c @@ -137,7 +137,7 @@ int wt_tpl_fill_scan_entry_info(WT_JSON_TPL_SCAN_RADIO_t *rscan, memset(ch_width, 0, sizeof(ch_width)); if(e->vht_chwidth == 0) { // If VHT width is 0, consider it as HT - if(e->ht_chwidth == 0 || e->ht_chwidth == 1) { + if(e->ht_chwidth < ARRAY_SIZE(ht_chanwidth)) { // Populate secondary channel char sc = 0; switch(e->ht_secondary) { @@ -159,9 +159,10 @@ int wt_tpl_fill_scan_entry_info(WT_JSON_TPL_SCAN_RADIO_t *rscan, strcpy(ch_width, "unknown"); } } else if(e->vht_chwidth >= 1 && - e->vht_chwidth < (sizeof(vht_chanwidth)/sizeof(vht_chanwidth[0]))) { + e->vht_chwidth < ARRAY_SIZE(vht_chanwidth)) { // Populate Channel width - strcpy(ch_width, vht_chanwidth[e->vht_chwidth]); + strncpy(ch_width, vht_chanwidth[e->vht_chwidth], sizeof(ch_width)); + ch_width[sizeof(ch_width)-1] = '\0'; // Populate secondary channels if they exist extras_obj[0].val.s = chanspec; if(e->vht_secondary1 != 0 && e->vht_secondary2 != 0) { diff --git a/src/unum/wireless/wireless.c b/src/unum/wireless/wireless.c index 288542ddb..9edba70a0 100644 --- a/src/unum/wireless/wireless.c +++ b/src/unum/wireless/wireless.c @@ -410,10 +410,6 @@ static void wireless_do_radio_telemetry(void) "Accept: application/json\0", jstr, strlen(jstr)); - // No longer need the JSON string - util_free_json_str(jstr); - jstr = NULL; - if(rsp == NULL || (rsp->code / 100) != 2) { log("%s: request error, code %d%s\n", __func__, rsp ? rsp->code : 0, rsp ? "" : "(none)"); @@ -486,10 +482,6 @@ static void wireless_do_scan_report(void) "Accept: application/json\0", jstr, strlen(jstr)); - // No longer need the JSON string - util_free_json_str(jstr); - jstr = NULL; - if(rsp == NULL || (rsp->code / 100) != 2) { log("%s: request error, code %d%s\n", __func__, rsp ? rsp->code : 0, rsp ? "" : "(none)"); diff --git a/src/unum/zip/ioapi.c b/src/unum/zip/ioapi.c index 7f5c191b2..5a61df6a8 100644 --- a/src/unum/zip/ioapi.c +++ b/src/unum/zip/ioapi.c @@ -30,39 +30,44 @@ voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode) { - if (pfilefunc->zfile_func64.zopen64_file != NULL) + if (pfilefunc->zfile_func64.zopen64_file != NULL) { return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode); - else - { + } else if (pfilefunc->zopen32_file != NULL) { return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode); + } else { + return NULL; } } long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin) { - if (pfilefunc->zfile_func64.zseek64_file != NULL) + if (pfilefunc->zfile_func64.zseek64_file != NULL) { return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin); - else - { + } else if (pfilefunc->zseek32_file != NULL) { uLong offsetTruncated = (uLong)offset; - if (offsetTruncated != offset) + if (offsetTruncated != offset) { return -1; - else + } else { return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin); + } + } else { + return -1; } } ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream) { - if (pfilefunc->zfile_func64.zseek64_file != NULL) + if (pfilefunc->zfile_func64.zseek64_file != NULL) { return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream); - else - { + } else if (pfilefunc->ztell32_file != NULL) { uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream); - if ((tell_uLong) == MAXU32) + if ((tell_uLong) == MAXU32) { return (ZPOS64_T)-1; - else + } else { return tell_uLong; + } + } else { + return (ZPOS64_T)-1; } }