Fix some minor issues found by cppcheck.
authorGuus Sliepen <guus@tinc-vpn.org>
Tue, 18 Apr 2017 18:09:38 +0000 (20:09 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Tue, 18 Apr 2017 18:09:38 +0000 (20:09 +0200)
src/invitation.c
src/protocol_key.c
src/route.c
src/tincctl.c
src/top.c

index 9a082e3..ac410b8 100644 (file)
@@ -252,8 +252,8 @@ int cmd_invite(int argc, char *argv[]) {
        }
 
        // If a daemon is running, ensure no other nodes know about this name
-       bool found = false;
        if(connect_tincd(false)) {
+               bool found = false;
                sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
 
                while(recvline(fd, line, sizeof line)) {
@@ -686,7 +686,7 @@ make_names:
                }
 
                // Copy the safe variable to the right config file
-               fprintf(variables[i].type & VAR_HOST ? fh : f, "%s = %s\n", l, value);
+               fprintf((variables[i].type & VAR_HOST) ? fh : f, "%s = %s\n", l, value);
        }
 
        fclose(f);
index d24d4ac..e1bb3b9 100644 (file)
@@ -356,7 +356,7 @@ bool ans_key_h(connection_t *c, const char *request) {
        char key[MAX_STRING_SIZE];
        char address[MAX_STRING_SIZE] = "";
        char port[MAX_STRING_SIZE] = "";
-       int cipher, digest, maclength, compression, keylen;
+       int cipher, digest, maclength, compression;
        node_t *from, *to;
 
        if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
@@ -489,7 +489,7 @@ bool ans_key_h(connection_t *c, const char *request) {
 
        /* Process key */
 
-       keylen = hex2bin(key, key, sizeof key);
+       int keylen = hex2bin(key, key, sizeof key);
 
        if(keylen != (from->outcipher ? cipher_keylength(from->outcipher) : 1)) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
index c10fddb..7ae4960 100644 (file)
@@ -510,7 +510,7 @@ static void route_broadcast(node_t *source, vpn_packet_t *packet) {
 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
        struct ip ip;
        vpn_packet_t fragment;
-       int len, maxlen, todo;
+       int maxlen, todo;
        uint8_t *offset;
        uint16_t ip_off, origf;
 
@@ -537,7 +537,7 @@ static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t et
        ip_off &= IP_OFFMASK;
 
        while(todo) {
-               len = todo > maxlen ? maxlen : todo;
+               int len = todo > maxlen ? maxlen : todo;
                memcpy(DATA(&fragment) + ether_size + ip_size, offset, len);
                todo -= len;
                offset += len;
index 6f4fb93..6416ebe 100644 (file)
@@ -513,7 +513,7 @@ bool recvline(int fd, char *line, size_t len) {
        char *newline = NULL;
 
        if(!fd)
-               abort();
+               return false;
 
        while(!(newline = memchr(buffer, '\n', blen))) {
                int result = recv(fd, buffer + blen, sizeof buffer - blen, 0);
@@ -951,11 +951,11 @@ static int cmd_stop(int argc, char *argv[]) {
        if(!connect_tincd(true)) {
                if(pid) {
                        if(kill(pid, SIGTERM)) {
-                               fprintf(stderr, "Could not send TERM signal to process with PID %u: %s\n", pid, strerror(errno));
+                               fprintf(stderr, "Could not send TERM signal to process with PID %d: %s\n", pid, strerror(errno));
                                return 1;
                        }
 
-                       fprintf(stderr, "Sent TERM signal to process with PID %u.\n", pid);
+                       fprintf(stderr, "Sent TERM signal to process with PID %d.\n", pid);
                        waitpid(pid, NULL, 0);
                        return 0;
                }
@@ -1030,7 +1030,6 @@ static int dump_invitations(void) {
                FILE *f = fopen(fname, "r");
                if(!f) {
                        fprintf(stderr, "Cannot open %s: %s\n", fname, strerror(errno));
-                       fclose(f);
                        continue;
                }
 
@@ -2826,8 +2825,6 @@ static int cmd_shell(int argc, char *argv[]) {
 
                while(p && *p) {
                        if(nargc >= maxargs) {
-                               fprintf(stderr, "next %p '%s', p %p '%s'\n", next, next, p, p);
-                               abort();
                                maxargs *= 2;
                                nargv = xrealloc(nargv, maxargs * sizeof *nargv);
                        }
index e517b4f..fe6f384 100644 (file)
--- a/src/top.c
+++ b/src/top.c
@@ -158,40 +158,47 @@ static int cmpu64(uint64_t a, uint64_t b) {
 static int sortfunc(const void *a, const void *b) {
        const nodestats_t *na = *(const nodestats_t **)a;
        const nodestats_t *nb = *(const nodestats_t **)b;
+       int result;
+
        switch(sortmode) {
                case 1:
                        if(cumulative)
-                               return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
+                               result = -cmpu64(na->in_packets, nb->in_packets);
                        else
-                               return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
+                               result = -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
                case 2:
                        if(cumulative)
-                               return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
+                               result = -cmpu64(na->in_bytes, nb->in_bytes);
                        else
-                               return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
+                               result = -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
                case 3:
                        if(cumulative)
-                               return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
+                               result = -cmpu64(na->out_packets, nb->out_packets);
                        else
-                               return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
+                               result = -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
                case 4:
                        if(cumulative)
-                               return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
+                               result = -cmpu64(na->out_bytes, nb->out_bytes);
                        else
-                               return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
+                               result = -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
                case 5:
                        if(cumulative)
-                               return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
+                               result = -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
                        else
-                               return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
+                               result = -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
                case 6:
                        if(cumulative)
-                               return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
+                               result = -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
                        else
-                               return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
+                               result = -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
                default:
-                       return strcmp(na->name, nb->name) ?: na->i - nb->i;
+                       result = strcmp(na->name, nb->name);
        }
+
+       if(result)
+               return result;
+       else
+               return na->i - nb->i;
 }
 
 static void redraw(void) {