Skip to content

Commit

Permalink
unum: misc code cleanup from coverity #2 (#50)
Browse files Browse the repository at this point in the history
MIN-10478
  • Loading branch information
Tom-Keddie authored Dec 21, 2021
1 parent 076a537 commit 90f0fa8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
24 changes: 12 additions & 12 deletions src/unum/http/http_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ static http_rsp *http_req(char *url, char *headers,

rsp = alloc_rsp(NULL, RSP_BUF_SIZE);
if(!rsp) {
log("%s: url <%s>, error allocating response buffer\n");
log("%s: url <%s>, error allocating response buffer\n", __func__, url);
return NULL;
}

ch = curl_easy_init();
if(!ch) {
free_rsp(rsp);
log("%s: url <%s>, error curl_easy_init() has failed\n");
log("%s: url <%s>, error curl_easy_init() has failed\n", __func__, url);
return NULL;
}

Expand All @@ -240,7 +240,7 @@ static http_rsp *http_req(char *url, char *headers,
typestr = "UNKNOWN";
}

log("%s: %08x %s url <%s>\n", __func__, ch, typestr, url);
log("%s: %p %s url <%s>\n", __func__, ch, typestr, url);

curl_easy_setopt(ch, CURLOPT_URL, url);
curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1);
Expand Down Expand Up @@ -296,7 +296,7 @@ static http_rsp *http_req(char *url, char *headers,
struct curl_slist *slold = NULL;
for(hdr = headers; hdr && *hdr != 0; hdr += strlen(hdr) + 1)
{
log("%s: %08x hdr: '%s'\n", __func__, ch, hdr);
log("%s: %p hdr: '%s'\n", __func__, ch, hdr);
slold = slhdr;
if((slhdr = curl_slist_append(slhdr, hdr)) == NULL) {
break;
Expand All @@ -307,7 +307,7 @@ static http_rsp *http_req(char *url, char *headers,
slhdr = curl_slist_append(slold, "Content-Encoding: gzip");
}
if(slhdr == NULL) {
log("%s: %08x failed to add headers, ignoring\n", __func__, ch);
log("%s: %p failed to add headers, ignoring\n", __func__, ch);
if(slold) {
curl_slist_free_all(slold);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ static http_rsp *http_req(char *url, char *headers,
if(((type & HTTP_REQ_TYPE_MASK) == HTTP_REQ_TYPE_POST) ||
((type & HTTP_REQ_TYPE_MASK) == HTTP_REQ_TYPE_PUT))
{
log("%s: %08x len%s: %d, ptr: '%.*s%s'\n",
log("%s: %p len%s: %d, ptr: '%.*s%s'\n",
__func__, ch, (compressed ? "(gzip)" : ""), dlen,
(len > MAX_LOG_DATA_LEN ? MAX_LOG_DATA_LEN : len), data,
(len > MAX_LOG_DATA_LEN ? "..." : ""));
Expand All @@ -367,27 +367,27 @@ static http_rsp *http_req(char *url, char *headers,
{
err = curl_easy_perform(ch);
if(err) {
log("%s: %08x error (%d) %s\n",
log("%s: %p error (%d) %s\n",
__func__, ch, err, curl_easy_strerror(err));
log("%s: %08x error info: %s\n", __func__, ch, err_buf);
log("%s: %p error info: %s\n", __func__, ch, err_buf);
// Refresh DNS servers, it might help with libc stale/cached DNS servers
res_init();
continue;
}

err = curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &resp_code);
if(err) {
log("%s: %08x getinfo error (%d) %s\n",
log("%s: %p getinfo error (%d) %s\n",
__func__, ch, err, curl_easy_strerror(err));
log("%s: %08x getinfo error info: %s\n", __func__, ch, err_buf);
log("%s: %p getinfo error info: %s\n", __func__, ch, err_buf);
break;
}

log("%s: %08x OK, reply: '%.*s%s'\n", __func__, ch,
log("%s: %p OK, reply: '%.*s%s'\n", __func__, ch,
(rsp->len > MAX_LOG_DATA_LEN ? MAX_LOG_DATA_LEN : rsp->len),
rsp->data,
(rsp->len > MAX_LOG_DATA_LEN ? "..." : ""));
log("%s: %08x rsp code: %ld\n", __func__, ch, resp_code);
log("%s: %p rsp code: %ld\n", __func__, ch, resp_code);
rsp->code = resp_code;

break;
Expand Down
22 changes: 13 additions & 9 deletions src/unum/log/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,25 @@ static int init_log_entry(LOG_DST_t dst)
}

// Quick check if we should even try to take mutex
if((lc->flags & (LOG_FLAG_INIT_DONE | LOG_FLAG_INIT_FAIL)) != 0) {
if(lc && (lc->flags & (LOG_FLAG_INIT_DONE | LOG_FLAG_INIT_FAIL)) != 0) {
return 0;
}

if((lc->flags & LOG_FLAG_MUTEX) != 0) {
if(lc && (lc->flags & LOG_FLAG_MUTEX) != 0) {
UTIL_MUTEX_TAKE(&(lc->m));
mutex_taken = TRUE;
}

for(;;)
{
// MT safe check if init is done
if((lc->flags & (LOG_FLAG_INIT_DONE | LOG_FLAG_INIT_FAIL)) != 0) {
if(lc && (lc->flags & (LOG_FLAG_INIT_DONE | LOG_FLAG_INIT_FAIL)) != 0) {
break;
}

#ifdef LOG_FLAG_FILE
// Cleanup stale log files if the log settings change between images
if(lc->max_size && lc->max && (lc->flags & LOG_FLAG_FILE) != 0)
if(lc && lc->max_size && lc->max && (lc->flags & LOG_FLAG_FILE) != 0)
{
int ii;
for(ii = lc->max + 1; ii <= LOG_ROTATE_CLEANUP_MAX; ii++)
Expand All @@ -163,7 +163,7 @@ static int init_log_entry(LOG_DST_t dst)
mask |= LOG_FLAG_TTY;
#endif // LOG_FLAG_TTY

if(!lc->f && (lc->flags & mask) != 0)
if(lc && !lc->f && (lc->flags & mask) != 0)
{
char fn[LOG_MAX_PATH + 1];
int fn_len;
Expand Down Expand Up @@ -202,14 +202,14 @@ static int init_log_entry(LOG_DST_t dst)

// If init is still not done, then either the platform does not support
// the entry's type of logging or the init has failed.
if((lc->flags & LOG_FLAG_INIT_DONE) == 0)
if(lc && (lc->flags & LOG_FLAG_INIT_DONE) == 0)
{
ret = -2;
break;
}

// Log startup message
if((lc->flags & LOG_FLAG_INIT_MSG) != 0 &&
if(lc && (lc->flags & LOG_FLAG_INIT_MSG) != 0 &&
(lc->flags & LOG_FLAG_INIT_DONE) != 0)
{
// Make sure this call is made only if the init function
Expand Down Expand Up @@ -237,8 +237,6 @@ void unum_log(LOG_DST_t dst, char *str, ...)
long fpos;
va_list ap;

va_start(ap, str);

// If process log destination override is set, use it
if(proc_log_dst_id >= LOG_DST_STDOUT && proc_log_dst_id < LOG_DST_MAX) {
dst = proc_log_dst_id;
Expand All @@ -248,6 +246,8 @@ void unum_log(LOG_DST_t dst, char *str, ...)
return;
}

va_start(ap, str);

// leave lc == NULL if unknown destination or logging to stdout
if(dst > LOG_DST_STDOUT && dst < LOG_DST_MAX) {
lc = &(log_cfg[dst]);
Expand All @@ -256,6 +256,7 @@ void unum_log(LOG_DST_t dst, char *str, ...)
// If no dst or if logging to stdout printf and leave
if(!lc || (lc->flags & LOG_FLAG_STDOUT) != 0) {
vprintf(str, ap);
va_end(ap);
return;
}

Expand All @@ -265,6 +266,7 @@ void unum_log(LOG_DST_t dst, char *str, ...)
// If logging entry failed to init, just printf and leave
if((lc->flags & LOG_FLAG_INIT_FAIL) != 0) {
vprintf(str, ap);
va_end(ap);
return;
}

Expand Down Expand Up @@ -366,6 +368,8 @@ void unum_log(LOG_DST_t dst, char *str, ...)
break;
}

va_end(ap);

if(mutex_taken) {
UTIL_MUTEX_GIVE(&(lc->m));
}
Expand Down

0 comments on commit 90f0fa8

Please sign in to comment.