Skip to content

Commit

Permalink
unum: misc code cleanup from coverity #3 (#51)
Browse files Browse the repository at this point in the history
MIN-10478
  • Loading branch information
Tom-Keddie authored Dec 21, 2021
1 parent 90f0fa8 commit f17be6f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 45 deletions.
6 changes: 1 addition & 5 deletions src/unum/speedtest/speedtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/unum/util/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() */
Expand Down Expand Up @@ -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() */
Expand All @@ -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() */
Expand Down
20 changes: 11 additions & 9 deletions src/unum/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
{
Expand All @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/unum/util/util_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions src/unum/wireless/nl80211_platform/scan_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
8 changes: 0 additions & 8 deletions src/unum/wireless/wireless.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Expand Down Expand Up @@ -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)");
Expand Down
31 changes: 18 additions & 13 deletions src/unum/zip/ioapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit f17be6f

Please sign in to comment.