From 87a0718ca09f158b36a079a3bb95401ba1f578b4 Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Sun, 13 May 2018 13:50:46 -0400 Subject: [PATCH] More bug fixes from sonar. (#500) --- src/apcupsd.cc | 43 ++++++++---------------- src/net_stat.cc | 82 ++++++++++++---------------------------------- src/tcp-portmon.cc | 14 +++++--- 3 files changed, 44 insertions(+), 95 deletions(-) diff --git a/src/apcupsd.cc b/src/apcupsd.cc index aa687a0d..83065465 100644 --- a/src/apcupsd.cc +++ b/src/apcupsd.cc @@ -26,11 +26,11 @@ #include "logging.h" #include "text_object.h" -#include #include #include #include #include +#include enum _apcupsd_items { APCUPSD_NAME, @@ -72,9 +72,7 @@ static int net_recv_ex(int sock, void *buf, int size, struct timeval *tv) { FD_SET(sock, &fds); res = select(sock + 1, &fds, nullptr, nullptr, tv); } while (res < 0 && errno == EINTR); - if (res < 0) { - return 0; - } + if (res < 0) { return 0; } if (res == 0) { // timeout errno = ETIMEDOUT; // select was succesfull, errno is now 0 @@ -86,9 +84,7 @@ static int net_recv_ex(int sock, void *buf, int size, struct timeval *tv) { errno = 0; res = recv(sock, static_cast(buf), size, 0); } while (res < 0 && errno == EINTR); - if (res < 0) { - return 0; - } + if (res < 0) { return 0; } if (res == 0) { // orderly shutdown errno = ENOTCONN; @@ -109,9 +105,7 @@ static int net_recv(int sock, void *buf, int size) { while (todo != 0) { len = net_recv_ex(sock, static_cast(buf) + off, todo, &tv); - if (len == 0) { - return 0; - } + if (len == 0) { return 0; } todo -= len; off += len; } @@ -124,13 +118,9 @@ static int net_recv(int sock, void *buf, int size) { static int get_line(int sock, char line[], short linesize) { // get the line length short sz; - if (net_recv(sock, &sz, sizeof(sz)) == 0) { - return -1; - } + if (net_recv(sock, &sz, sizeof(sz)) == 0) { return -1; } sz = ntohs(sz); - if (sz == 0) { - return 0; - } + if (sz == 0) { return 0; } // get the line while (sz >= linesize) { @@ -138,9 +128,7 @@ static int get_line(int sock, char line[], short linesize) { net_recv(sock, line, linesize); sz -= linesize; } - if (net_recv(sock, line, sz) == 0) { - return 0; - } + if (net_recv(sock, line, sz) == 0) { return 0; } line[sz] = 0; return sz; } @@ -191,7 +179,7 @@ static int fill_items(int sock, PAPCUPSD_S apc) { int update_apcupsd() { int i; APCUPSD_S apc; - int sock; + int sock = -1; for (i = 0; i < _APCUPSD_COUNT; ++i) { memcpy(apc.items[i], "N/A", 4); // including \0 @@ -219,18 +207,15 @@ int update_apcupsd() { } for (rp = ai; rp != nullptr; rp = rp->ai_next) { sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sock == -1) { - continue; - } - if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) { - break; - } + if (sock == -1) { continue; } + if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) { break; } close(sock); + sock = -1; } freeaddrinfo(ai); if (rp == nullptr) { // no error reporting, the daemon is probably not running - close(sock); + if (sock >= 0) { close(sock); } break; } @@ -266,9 +251,7 @@ int update_apcupsd() { int apcupsd_scan_arg(const char *arg) { char host[64]; int port; - if (sscanf(arg, "%63s %d", host, &port) != 2) { - return 1; - } + if (sscanf(arg, "%63s %d", host, &port) != 2) { return 1; } apcupsd.port = port; strncpy(apcupsd.host, host, sizeof(apcupsd.host)); diff --git a/src/net_stat.cc b/src/net_stat.cc index 89caebff..6bee5ea1 100644 --- a/src/net_stat.cc +++ b/src/net_stat.cc @@ -27,7 +27,6 @@ * */ -#include "net_stat.h" #include #include #include @@ -35,6 +34,7 @@ #include "conky.h" #include "logging.h" #include "net/if.h" +#include "net_stat.h" #include "specials.h" #include "text_object.h" #if defined(__sun) @@ -45,7 +45,7 @@ #define IFF_RUNNING IFF_LINK #endif #ifndef SOCK_CLOEXEC -# define SOCK_CLOEXEC O_CLOEXEC +#define SOCK_CLOEXEC O_CLOEXEC #endif /* SOCK_CLOEXEC */ /* network interface stuff */ @@ -77,9 +77,7 @@ struct net_stat *get_net_stat(const char *dev, void * /*free_at_crash1*/, void * /*free_at_crash2*/) { unsigned int i; - if (dev == nullptr) { - return nullptr; - } + if (dev == nullptr) { return nullptr; } /* find interface stat */ for (i = 0; i < MAX_NET_INTERFACES; i++) { @@ -117,9 +115,7 @@ void parse_net_stat_arg(struct text_object *obj, const char *arg, int i = 0; struct net_stat *netstat = nullptr; - if (arg == nullptr) { - arg = DEFAULTNETDEV; - } + if (arg == nullptr) { arg = DEFAULTNETDEV; } while (sscanf(arg + i, " %20s", nextarg) == 1) { if (strcmp(nextarg, "-n") == 0 || strcmp(nextarg, "--netmask") == 0) { @@ -128,12 +124,8 @@ void parse_net_stat_arg(struct text_object *obj, const char *arg, showscope = true; } else if (nextarg[0] == '-') { // multiple flags in 1 arg for (int j = 1; nextarg[j] != 0; j++) { - if (nextarg[j] == 'n') { - shownetmask = true; - } - if (nextarg[j] == 's') { - showscope = true; - } + if (nextarg[j] == 'n') { shownetmask = true; } + if (nextarg[j] == 's') { showscope = true; } } } else { netstat = get_net_stat(nextarg, obj, free_at_crash); @@ -170,9 +162,7 @@ void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void print_downspeed(struct text_object *obj, char *p, int p_max_size) { auto *ns = static_cast(obj->data.opaque); - if (ns == nullptr) { - return; - } + if (ns == nullptr) { return; } human_readable(ns->recv_speed, p, p_max_size); } @@ -180,9 +170,7 @@ void print_downspeed(struct text_object *obj, char *p, int p_max_size) { void print_downspeedf(struct text_object *obj, char *p, int p_max_size) { auto *ns = static_cast(obj->data.opaque); - if (ns == nullptr) { - return; - } + if (ns == nullptr) { return; } spaced_print(p, p_max_size, "%.1f", 8, ns->recv_speed / 1024.0); } @@ -190,9 +178,7 @@ void print_downspeedf(struct text_object *obj, char *p, int p_max_size) { void print_upspeed(struct text_object *obj, char *p, int p_max_size) { auto *ns = static_cast(obj->data.opaque); - if (ns == nullptr) { - return; - } + if (ns == nullptr) { return; } human_readable(ns->trans_speed, p, p_max_size); } @@ -200,9 +186,7 @@ void print_upspeed(struct text_object *obj, char *p, int p_max_size) { void print_upspeedf(struct text_object *obj, char *p, int p_max_size) { auto *ns = static_cast(obj->data.opaque); - if (ns == nullptr) { - return; - } + if (ns == nullptr) { return; } spaced_print(p, p_max_size, "%.1f", 8, ns->trans_speed / 1024.0); } @@ -210,9 +194,7 @@ void print_upspeedf(struct text_object *obj, char *p, int p_max_size) { void print_totaldown(struct text_object *obj, char *p, int p_max_size) { auto *ns = static_cast(obj->data.opaque); - if (ns == nullptr) { - return; - } + if (ns == nullptr) { return; } human_readable(ns->recv, p, p_max_size); } @@ -220,9 +202,7 @@ void print_totaldown(struct text_object *obj, char *p, int p_max_size) { void print_totalup(struct text_object *obj, char *p, int p_max_size) { auto *ns = static_cast(obj->data.opaque); - if (ns == nullptr) { - return; - } + if (ns == nullptr) { return; } human_readable(ns->trans, p, p_max_size); } @@ -230,9 +210,7 @@ void print_totalup(struct text_object *obj, char *p, int p_max_size) { void print_addr(struct text_object *obj, char *p, int p_max_size) { auto *ns = static_cast(obj->data.opaque); - if (ns == nullptr) { - return; - } + if (ns == nullptr) { return; } if ((ns->addr.sa_data[2] & 255) == 0 && (ns->addr.sa_data[3] & 255) == 0 && (ns->addr.sa_data[4] & 255) == 0 && (ns->addr.sa_data[5] & 255) == 0) { @@ -264,8 +242,6 @@ void print_v6addrs(struct text_object *obj, char *p, int p_max_size) { char tempaddress[INET6_ADDRSTRLEN]; struct v6addr *current_v6 = ns->v6addrs; - if (ns == nullptr) return; - if (p_max_size == 0) return; if (!ns->v6addrs) { snprintf(p, p_max_size, "No Address"); @@ -487,9 +463,7 @@ int interface_up(struct text_object *obj) { struct ifreq ifr {}; auto *dev = static_cast(obj->data.opaque); - if (dev == nullptr) { - return 0; - } + if (dev == nullptr) { return 0; } if ((fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) { CRIT_ERR(nullptr, nullptr, "could not create sockfd"); @@ -498,27 +472,19 @@ int interface_up(struct text_object *obj) { strncpy(ifr.ifr_name, dev, IFNAMSIZ); if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) { /* if device does not exist, treat like not up */ - if (errno != ENODEV && errno != ENXIO) { - perror("SIOCGIFFLAGS"); - } + if (errno != ENODEV && errno != ENXIO) { perror("SIOCGIFFLAGS"); } goto END_FALSE; } if ((ifr.ifr_flags & IFF_UP) == 0) { /* iface is not up */ goto END_FALSE; } - if (if_up_strictness.get(*state) == IFUP_UP) { - goto END_TRUE; - } + if (if_up_strictness.get(*state) == IFUP_UP) { goto END_TRUE; } #ifdef IFF_RUNNING - if ((ifr.ifr_flags & IFF_RUNNING) == 0) { - goto END_FALSE; - } + if ((ifr.ifr_flags & IFF_RUNNING) == 0) { goto END_FALSE; } #endif - if (if_up_strictness.get(*state) == IFUP_LINK) { - goto END_TRUE; - } + if (if_up_strictness.get(*state) == IFUP_LINK) { goto END_TRUE; } if (ioctl(fd, SIOCGIFADDR, &ifr) != 0) { perror("SIOCGIFADDR"); @@ -550,9 +516,7 @@ void free_dns_data(struct text_object *obj) { (void)obj; - for (i = 0; i < dns_data.nscount; i++) { - free(dns_data.ns_list[i]); - } + for (i = 0; i < dns_data.nscount; i++) { free(dns_data.ns_list[i]); } free(dns_data.ns_list); memset(&dns_data, 0, sizeof(dns_data)); } @@ -570,13 +534,9 @@ int update_dns_data() { free_dns_data(nullptr); - if ((fp = fopen("/etc/resolv.conf", "re")) == nullptr) { - return 0; - } + if ((fp = fopen("/etc/resolv.conf", "re")) == nullptr) { return 0; } while (feof(fp) == 0) { - if (fgets(line, 255, fp) == nullptr) { - break; - } + if (fgets(line, 255, fp) == nullptr) { break; } if (strncmp(line, "nameserver ", 11) == 0) { line[strlen(line) - 1] = '\0'; // remove trailing newline dns_data.nscount++; diff --git a/src/tcp-portmon.cc b/src/tcp-portmon.cc index 843341ea..00d1c53c 100644 --- a/src/tcp-portmon.cc +++ b/src/tcp-portmon.cc @@ -17,10 +17,10 @@ * along with this program. If not, see . * */ -#include "tcp-portmon.h" #include "conky.h" #include "libtcp-portmon.h" #include "logging.h" +#include "tcp-portmon.h" #include "text_object.h" static tcp_port_monitor_collection_t *pmc = nullptr; @@ -30,7 +30,11 @@ static conky::range_config_setting max_port_monitor_connections( MAX_PORT_MONITOR_CONNECTIONS_DEFAULT, false); int tcp_portmon_init(struct text_object *obj, const char *arg) { - int argc, port_begin, port_end, item, connection_index; + int item = -1; + int argc; + int port_begin; + int port_end; + int connection_index; char itembuf[32]; struct tcp_port_monitor_data *pmd; @@ -47,7 +51,8 @@ int tcp_portmon_init(struct text_object *obj, const char *arg) { CRIT_ERR(nullptr, NULL, "tcp_portmon: port values must be from 1 to 65535"); } if (port_begin > port_end) { - CRIT_ERR(nullptr, NULL, "tcp_portmon: starting port must be <= ending port"); + CRIT_ERR(nullptr, NULL, + "tcp_portmon: starting port must be <= ending port"); } if (strncmp(itembuf, "count", 31) == EQUAL) { item = COUNT; @@ -76,7 +81,8 @@ int tcp_portmon_init(struct text_object *obj, const char *arg) { "item"); } if ((argc == 4) && (connection_index < 0)) { - CRIT_ERR(nullptr, NULL, "tcp_portmon: connection index must be non-negative"); + CRIT_ERR(nullptr, NULL, + "tcp_portmon: connection index must be non-negative"); } /* ok, args looks good. save the text object data */ pmd = (tcp_port_monitor_data *)malloc(sizeof(struct tcp_port_monitor_data));