From d35973833e6bf05fec29100a4d5e66bb07b06659 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 24 Apr 2018 16:39:35 -0700 Subject: fix a number of cppcheck complaints (not all) --- src/compat/nanomsg/nn.c | 23 +++++++++------------ src/core/message.c | 12 +++++------ src/core/msgqueue.c | 23 ++++++++------------- src/core/panic.c | 13 ++++++------ src/core/random.c | 10 ++++----- src/core/socket.c | 1 - src/core/taskq.c | 26 +++++++++++------------ src/core/timer.c | 5 ++--- src/platform/posix/posix_epdesc.c | 11 +++++----- src/platform/posix/posix_pipedesc.c | 4 ++-- src/platform/posix/posix_udp.c | 41 ++++++++++++++++++++----------------- src/supplemental/base64/base64.c | 5 ++--- src/transport/ws/websocket.c | 7 +++---- 13 files changed, 82 insertions(+), 99 deletions(-) (limited to 'src') diff --git a/src/compat/nanomsg/nn.c b/src/compat/nanomsg/nn.c index 40671cfa..c154ec6b 100644 --- a/src/compat/nanomsg/nn.c +++ b/src/compat/nanomsg/nn.c @@ -440,12 +440,11 @@ nn_recvmsg(int s, struct nn_msghdr *mh, int flags) keep = 1; // Do not discard message! } else { // copyout to multiple iovecs. - char * ptr = nng_msg_body(msg); - int i; - size_t n; - len = nng_msg_len(msg); + char *ptr = nng_msg_body(msg); + len = nng_msg_len(msg); - for (i = 0; i < mh->msg_iovlen; i++) { + for (int i = 0; i < mh->msg_iovlen; i++) { + size_t n; if ((n = mh->msg_iov[i].iov_len) == NN_MSG) { // This is forbidden! nn_seterror(NNG_EINVAL); @@ -475,7 +474,6 @@ nn_recvmsg(int s, struct nn_msghdr *mh, int flags) size_t tlen; size_t spsz; struct nn_cmsghdr *hdr; - unsigned char * ptr; spsz = nng_msg_header_len(msg); clen = NN_CMSG_SPACE(sizeof(spsz) + spsz); @@ -504,7 +502,7 @@ nn_recvmsg(int s, struct nn_msghdr *mh, int flags) } if (clen <= tlen) { - ptr = NN_CMSG_DATA(cdata); + uint8_t *ptr = NN_CMSG_DATA(cdata); hdr = (void *) cdata; hdr->cmsg_len = clen; hdr->cmsg_level = PROTO_SP; @@ -578,11 +576,10 @@ nn_sendmsg(int s, const struct nn_msghdr *mh, int flags) // usability we've ever seen. cmsg = NULL; if ((cdata = mh->msg_control) != NULL) { - size_t clen; - size_t offs; - size_t spsz; - struct nn_cmsghdr *chdr; - unsigned char * data; + size_t clen; + size_t offs; + size_t spsz; + unsigned char *data; if ((clen = mh->msg_controllen) == NN_MSG) { // Underlying data is a message. This is awkward, @@ -598,7 +595,7 @@ nn_sendmsg(int s, const struct nn_msghdr *mh, int flags) offs = 0; while ((offs + sizeof(NN_CMSG_LEN(0))) < clen) { - chdr = (void *) (cdata + offs); + struct nn_cmsghdr *chdr = (void *) (cdata + offs); if ((chdr->cmsg_level != PROTO_SP) || (chdr->cmsg_type != SP_HDR)) { offs += chdr->cmsg_len; diff --git a/src/core/message.c b/src/core/message.c index 35153f01..5c396f21 100644 --- a/src/core/message.c +++ b/src/core/message.c @@ -1,6 +1,6 @@ // -// Copyright 2017 Garrett D'Amore -// Copyright 2017 Capitar IT Group BV +// Copyright 2018 Staysail Systems, Inc. +// Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -102,7 +102,6 @@ nni_msg_dump(const char *banner, const nni_msg *msg) static int nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) { - size_t headroom = 0; uint8_t *newbuf; // We assume that if the pointer is a valid pointer, and inside @@ -121,7 +120,7 @@ nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) if ((ch->ch_ptr >= ch->ch_buf) && (ch->ch_ptr < (ch->ch_buf + ch->ch_cap))) { - headroom = (size_t)(ch->ch_ptr - ch->ch_buf); + size_t headroom = (size_t)(ch->ch_ptr - ch->ch_buf); if (headwanted < headroom) { headwanted = headroom; // Never shrink this. } @@ -474,10 +473,9 @@ nni_msg_getopt(nni_msg *m, int opt, void *val, size_t *szp) int nni_msg_realloc(nni_msg *m, size_t sz) { - int rv = 0; - if (m->m_body.ch_len < sz) { - rv = nni_chunk_append(&m->m_body, NULL, sz - m->m_body.ch_len); + int rv = + nni_chunk_append(&m->m_body, NULL, sz - m->m_body.ch_len); if (rv != 0) { return (rv); } diff --git a/src/core/msgqueue.c b/src/core/msgqueue.c index a4a7ff36..2367b57f 100644 --- a/src/core/msgqueue.c +++ b/src/core/msgqueue.c @@ -82,8 +82,6 @@ nni_msgq_init(nni_msgq **mqp, unsigned cap) void nni_msgq_fini(nni_msgq *mq) { - nni_msg *msg; - if (mq == NULL) { return; } @@ -91,7 +89,7 @@ nni_msgq_fini(nni_msgq *mq) /* Free any orphaned messages. */ while (mq->mq_len > 0) { - msg = mq->mq_msgs[mq->mq_get]; + nni_msg *msg = mq->mq_msgs[mq->mq_get]; mq->mq_get++; if (mq->mq_get >= mq->mq_alloc) { mq->mq_get = 0; @@ -114,8 +112,6 @@ nni_msgq_fini(nni_msgq *mq) void nni_msgq_set_get_error(nni_msgq *mq, int error) { - nni_aio *aio; - // Let all pending blockers know we are closing the queue. nni_mtx_lock(&mq->mq_lock); if (mq->mq_closed) { @@ -123,6 +119,7 @@ nni_msgq_set_get_error(nni_msgq *mq, int error) error = NNG_ECLOSED; } if (error != 0) { + nni_aio *aio; while ((aio = nni_list_first(&mq->mq_aio_getq)) != NULL) { nni_aio_list_remove(aio); nni_aio_finish_error(aio, error); @@ -136,8 +133,6 @@ nni_msgq_set_get_error(nni_msgq *mq, int error) void nni_msgq_set_put_error(nni_msgq *mq, int error) { - nni_aio *aio; - // Let all pending blockers know we are closing the queue. nni_mtx_lock(&mq->mq_lock); if (mq->mq_closed) { @@ -145,6 +140,7 @@ nni_msgq_set_put_error(nni_msgq *mq, int error) error = NNG_ECLOSED; } if (error != 0) { + nni_aio *aio; while ((aio = nni_list_first(&mq->mq_aio_putq)) != NULL) { nni_aio_list_remove(aio); nni_aio_finish_error(aio, error); @@ -158,8 +154,6 @@ nni_msgq_set_put_error(nni_msgq *mq, int error) void nni_msgq_set_error(nni_msgq *mq, int error) { - nni_aio *aio; - // Let all pending blockers know we are closing the queue. nni_mtx_lock(&mq->mq_lock); if (mq->mq_closed) { @@ -167,6 +161,7 @@ nni_msgq_set_error(nni_msgq *mq, int error) error = NNG_ECLOSED; } if (error != 0) { + nni_aio *aio; while (((aio = nni_list_first(&mq->mq_aio_getq)) != NULL) || ((aio = nni_list_first(&mq->mq_aio_putq)) != NULL)) { nni_aio_list_remove(aio); @@ -207,13 +202,11 @@ static void nni_msgq_run_putq(nni_msgq *mq) { nni_aio *waio; - nni_aio *raio; - nni_msg *msg; - size_t len; while ((waio = nni_list_first(&mq->mq_aio_putq)) != NULL) { - msg = nni_aio_get_msg(waio); - len = nni_msg_len(msg); + nni_msg *msg = nni_aio_get_msg(waio); + size_t len = nni_msg_len(msg); + nni_aio *raio; // The presence of any blocked reader indicates that // the queue is empty, otherwise it would have just taken @@ -257,9 +250,9 @@ static void nni_msgq_run_getq(nni_msgq *mq) { nni_aio *raio; - nni_aio *waio; while ((raio = nni_list_first(&mq->mq_aio_getq)) != NULL) { + nni_aio *waio; // If anything is waiting in the queue, get it first. if (mq->mq_len != 0) { nni_msg *msg = mq->mq_msgs[mq->mq_get++]; diff --git a/src/core/panic.c b/src/core/panic.c index f3c7cb26..ff621a59 100644 --- a/src/core/panic.c +++ b/src/core/panic.c @@ -1,5 +1,6 @@ // -// Copyright 2016 Garrett D'Amore +// Copyright 2018 Staysail Systems, Inc. +// Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -23,18 +24,16 @@ void nni_show_backtrace(void) { #if NNG_HAVE_BACKTRACE - void * frames[50]; - int nframes; - char **lines; - int i; + void *frames[50]; + int nframes; nframes = backtrace(frames, sizeof(frames) / sizeof(frames[0])); if (nframes > 1) { - lines = backtrace_symbols(frames, nframes); + char **lines = backtrace_symbols(frames, nframes); if (lines == NULL) { return; } - for (i = 1; i < nframes; i++) { + for (int i = 1; i < nframes; i++) { nni_println(lines[i]); } } diff --git a/src/core/random.c b/src/core/random.c index febd0848..2d899f21 100644 --- a/src/core/random.c +++ b/src/core/random.c @@ -1,5 +1,6 @@ // -// Copyright 2017 Garrett D'Amore +// Copyright 2018 Staysail Systems, Inc. +// Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -37,13 +38,12 @@ typedef struct { static void nni_isaac(nni_isaac_ctx *ctx) { - register uint32_t i, x, y; - ctx->cc++; // cc incremented once per 256 results ctx->bb += ctx->cc; // then combined with bb - for (i = 0; i < 256; ++i) { - x = ctx->mm[i]; + for (uint32_t i = 0; i < 256; ++i) { + uint32_t x = ctx->mm[i]; + uint32_t y; switch (i % 4) { case 0: ctx->aa ^= (ctx->aa << 13); diff --git a/src/core/socket.c b/src/core/socket.c index 8a14beb5..5a581cd3 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -81,7 +81,6 @@ struct nni_socket { nni_list s_pipes; // active pipes nni_list s_ctxs; // active contexts (protected by global nni_sock_lk) - int s_ep_pend; // EP dial/listen in progress int s_closing; // Socket is closing int s_closed; // Socket closed, protected by global lock bool s_ctxwait; // Waiting for contexts to close. diff --git a/src/core/taskq.c b/src/core/taskq.c index 90cf7e22..b0fe160b 100644 --- a/src/core/taskq.c +++ b/src/core/taskq.c @@ -1,6 +1,6 @@ // -// Copyright 2017 Garrett D'Amore -// Copyright 2017 Capitar IT Group BV +// Copyright 2018 Staysail Systems, Inc. +// Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -70,9 +70,7 @@ nni_taskq_thread(void *self) int nni_taskq_init(nni_taskq **tqp, int nthr) { - int rv; nni_taskq *tq; - int i; if ((tq = NNI_ALLOC_STRUCT(tq)) == NULL) { return (NNG_ENOMEM); @@ -89,7 +87,8 @@ nni_taskq_init(nni_taskq **tqp, int nthr) nni_cv_init(&tq->tq_sched_cv, &tq->tq_mtx); nni_cv_init(&tq->tq_wait_cv, &tq->tq_mtx); - for (i = 0; i < nthr; i++) { + for (int i = 0; i < nthr; i++) { + int rv; tq->tq_threads[i].tqt_tq = tq; tq->tq_threads[i].tqt_running = NULL; rv = nni_thr_init(&tq->tq_threads[i].tqt_thread, @@ -100,7 +99,7 @@ nni_taskq_init(nni_taskq **tqp, int nthr) } } tq->tq_run = 1; - for (i = 0; i < tq->tq_nthreads; i++) { + for (int i = 0; i < tq->tq_nthreads; i++) { nni_thr_run(&tq->tq_threads[i].tqt_thread); } *tqp = tq; @@ -190,20 +189,19 @@ void nni_task_wait(nni_task *task) { nni_taskq *tq = task->task_tq; - int running; if (task->task_cb == NULL) { return; } nni_mtx_lock(&tq->tq_mtx); for (;;) { - running = 0; + bool running = false; if (nni_list_active(&tq->tq_tasks, task)) { - running = 1; + running = true; } else { for (int i = 0; i < tq->tq_nthreads; i++) { if (tq->tq_threads[i].tqt_running == task) { - running = 1; + running = true; break; } } @@ -222,15 +220,15 @@ int nni_task_cancel(nni_task *task) { nni_taskq *tq = task->task_tq; - int running; + bool running; nni_mtx_lock(&tq->tq_mtx); - running = 1; + running = true; for (;;) { - running = 0; + running = false; for (int i = 0; i < tq->tq_nthreads; i++) { if (tq->tq_threads[i].tqt_running == task) { - running = 1; + running = true; break; } } diff --git a/src/core/timer.c b/src/core/timer.c index 08337b70..ad47ae33 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -103,8 +103,7 @@ nni_timer_cancel(nni_timer_node *node) void nni_timer_schedule(nni_timer_node *node, nni_time when) { - nni_timer * timer = &nni_global_timer; - nni_timer_node *srch; + nni_timer *timer = &nni_global_timer; nni_mtx_lock(&timer->t_mx); node->t_expire = when; @@ -114,7 +113,7 @@ nni_timer_schedule(nni_timer_node *node, nni_time when) } if (when != NNI_TIME_NEVER) { - srch = nni_list_first(&timer->t_entries); + nni_timer_node *srch = nni_list_first(&timer->t_entries); while ((srch != NULL) && (srch->t_expire < node->t_expire)) { srch = nni_list_next(&timer->t_entries, srch); } diff --git a/src/platform/posix/posix_epdesc.c b/src/platform/posix/posix_epdesc.c index 196b206a..f97f12b8 100644 --- a/src/platform/posix/posix_epdesc.c +++ b/src/platform/posix/posix_epdesc.c @@ -129,9 +129,9 @@ static void nni_posix_epdesc_doaccept(nni_posix_epdesc *ed) { nni_aio *aio; - int newfd; while ((aio = nni_list_first(&ed->acceptq)) != NULL) { + int newfd; #ifdef NNG_USE_ACCEPT4 newfd = accept4(ed->node.fd, NULL, NULL, SOCK_CLOEXEC); @@ -195,9 +195,8 @@ nni_posix_epdesc_doerror(nni_posix_epdesc *ed) static void nni_posix_epdesc_doclose(nni_posix_epdesc *ed) { - nni_aio * aio; - struct sockaddr_un *sun; - int fd; + nni_aio *aio; + int fd; ed->closed = true; while ((aio = nni_list_first(&ed->acceptq)) != NULL) { @@ -210,10 +209,10 @@ nni_posix_epdesc_doclose(nni_posix_epdesc *ed) nni_posix_pollq_remove(&ed->node); if ((fd = ed->node.fd) != -1) { - ed->node.fd = -1; + struct sockaddr_un *sun = (void *) &ed->locaddr; + ed->node.fd = -1; (void) shutdown(fd, SHUT_RDWR); (void) close(fd); - sun = (void *) &ed->locaddr; if ((sun->sun_family == AF_UNIX) && (ed->loclen != 0)) { (void) unlink(sun->sun_path); } diff --git a/src/platform/posix/posix_pipedesc.c b/src/platform/posix/posix_pipedesc.c index 62531f9c..72251372 100644 --- a/src/platform/posix/posix_pipedesc.c +++ b/src/platform/posix/posix_pipedesc.c @@ -202,8 +202,7 @@ nni_posix_pipedesc_doread(nni_posix_pipedesc *pd) static void nni_posix_pipedesc_cb(void *arg) { - nni_posix_pipedesc *pd = arg; - int events = 0; + nni_posix_pipedesc *pd = arg; nni_mtx_lock(&pd->mtx); if (pd->node.revents & POLLIN) { @@ -215,6 +214,7 @@ nni_posix_pipedesc_cb(void *arg) if (pd->node.revents & (POLLHUP | POLLERR | POLLNVAL)) { nni_posix_pipedesc_doclose(pd); } else { + int events = 0; if (!nni_list_empty(&pd->writeq)) { events |= POLLOUT; } diff --git a/src/platform/posix/posix_udp.c b/src/platform/posix/posix_udp.c index b414fa95..37f79431 100644 --- a/src/platform/posix/posix_udp.c +++ b/src/platform/posix/posix_udp.c @@ -143,25 +143,28 @@ nni_posix_udp_dosend(nni_plat_udp *udp) } #endif - for (unsigned i = 0; i < niov; i++) { - iov[i].iov_base = aiov[i].iov_buf; - iov[i].iov_len = aiov[i].iov_len; - } - hdr.msg_iov = iov; - hdr.msg_iovlen = niov; - hdr.msg_name = &ss; - hdr.msg_namelen = len; - hdr.msg_flags = NNI_MSG_NOSIGNAL; - hdr.msg_control = NULL; - hdr.msg_controllen = 0; - - if ((cnt = sendmsg(udp->udp_fd, &hdr, 0)) < 0) { - if ((errno == EAGAIN) || - (errno == EWOULDBLOCK)) { - // Cannot send now, leave at head. - return; + if (rv == 0) { + for (unsigned i = 0; i < niov; i++) { + iov[i].iov_base = aiov[i].iov_buf; + iov[i].iov_len = aiov[i].iov_len; + } + hdr.msg_iov = iov; + hdr.msg_iovlen = niov; + hdr.msg_name = &ss; + hdr.msg_namelen = len; + hdr.msg_flags = NNI_MSG_NOSIGNAL; + hdr.msg_control = NULL; + hdr.msg_controllen = 0; + + if ((cnt = sendmsg(udp->udp_fd, &hdr, 0)) < + 0) { + if ((errno == EAGAIN) || + (errno == EWOULDBLOCK)) { + // Cannot send now, leave. + return; + } + rv = nni_plat_errno(errno); } - rv = nni_plat_errno(errno); } } @@ -176,7 +179,6 @@ nni_posix_udp_cb(void *arg) { nni_plat_udp *udp = arg; int revents; - int events = 0; nni_mtx_lock(&udp->udp_mtx); revents = udp->udp_pitem.revents; @@ -189,6 +191,7 @@ nni_posix_udp_cb(void *arg) if (revents & (POLLHUP | POLLERR | POLLNVAL)) { nni_posix_udp_doclose(udp); } else { + int events = 0; if (!nni_list_empty(&udp->udp_sendq)) { events |= POLLOUT; } diff --git a/src/supplemental/base64/base64.c b/src/supplemental/base64/base64.c index afb50e1a..3f6d51b5 100644 --- a/src/supplemental/base64/base64.c +++ b/src/supplemental/base64/base64.c @@ -105,15 +105,14 @@ nni_base64_encode(const uint8_t *in, size_t in_len, char *out, size_t out_len) unsigned io; unsigned rem; uint32_t v; - uint8_t ch; const uint8_t ENCODEMAP[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; for (io = 0, ii = 0, v = 0, rem = 0; ii < in_len; ii++) { - ch = in[ii]; - v = (v << 8) | ch; + uint8_t ch = in[ii]; + v = (v << 8) | ch; rem += 8; while (rem >= 6) { rem -= 6; diff --git a/src/transport/ws/websocket.c b/src/transport/ws/websocket.c index 761ba824..9d967668 100644 --- a/src/transport/ws/websocket.c +++ b/src/transport/ws/websocket.c @@ -308,16 +308,15 @@ ws_ep_accept(void *arg, nni_aio *aio) static void ws_ep_connect(void *arg, nni_aio *aio) { - ws_ep * ep = arg; - int rv = 0; - ws_hdr *h; + ws_ep *ep = arg; if (nni_aio_begin(aio) != 0) { return; } if (!ep->started) { + ws_hdr *h; NNI_LIST_FOREACH (&ep->headers, h) { - rv = nni_ws_dialer_header( + int rv = nni_ws_dialer_header( ep->dialer, h->name, h->value); if (rv != 0) { nni_aio_finish_error(aio, rv); -- cgit v1.2.3-70-g09d2