Enable more clang-tidy checks
authorKirill Isakov <bootctl@gmail.com>
Mon, 23 May 2022 19:22:38 +0000 (01:22 +0600)
committerKirill Isakov <bootctl@gmail.com>
Mon, 23 May 2022 19:22:38 +0000 (01:22 +0600)
.clang-tidy
src/fsck.c
src/invitation.c
src/linux/uml_device.c
src/raw_socket_device.c
src/tincctl.c
src/tincd.c

index d0a89c1..d204cff 100644 (file)
@@ -1,3 +1,3 @@
-Checks: '-*,bugprone-narrowing-conversions'
-HeaderFilterRegex: '.*'
-WarningsAsErrors: '*'
+Checks: "-*,performance-*,modernize-*,misc-*,-misc-no-recursion,bugprone-*,-bugprone-macro-parentheses,-bugprone-easily-swappable-parameters,-bugprone-reserved-identifier,-bugprone-suspicious-string-compare,-bugprone-implicit-widening-of-multiplication-result,-bugprone-not-null-terminated-result,-bugprone-branch-clone,-bugprone-sizeof-expression,clang-analyzer-*,-clang-analyzer-security.insecureAPI.*,-clang-analyzer-core.UndefinedBinaryOperatorResult"
+HeaderFilterRegex: ".*"
+WarningsAsErrors: "*"
index 70ca84d..b44b775 100644 (file)
@@ -142,6 +142,11 @@ static void check_conffile(const char *nodename, bool server) {
                ++total_vars;
        }
 
+       if(!total_vars) {
+               splay_empty_tree(&config);
+               return;
+       }
+
        const size_t countlen = total_vars * sizeof(int);
        int *count = alloca(countlen);
        memset(count, 0, countlen);
index 5db1e98..151f701 100644 (file)
@@ -673,7 +673,7 @@ static char *get_value(const char *data, const char *var) {
 }
 
 static char *grep(const char *data, const char *var) {
-       static char value[1024];
+       char value[1024];
 
        const char *p = data;
        size_t varlen = strlen(var);
@@ -713,7 +713,7 @@ static char *grep(const char *data, const char *var) {
 
        memcpy(value, p, e - p);
        value[e - p] = 0;
-       return value;
+       return xstrdup(value);
 }
 
 static bool finalize_join(void) {
@@ -730,10 +730,10 @@ static bool finalize_join(void) {
        }
 
        if(!netname) {
-               const char *net = grep(data, "NetName");
+               char *net = grep(data, "NetName");
 
                if(net) {
-                       netname = xstrdup(net);
+                       netname = net;
 
                        if(!check_netname(netname, true)) {
                                fprintf(stderr, "Unsafe NetName found in invitation!\n");
index dafe5b2..2004850 100644 (file)
@@ -196,8 +196,6 @@ static void close_device(void) {
 }
 
 static bool read_packet(vpn_packet_t *packet) {
-       ssize_t inlen;
-
        switch(state) {
        case 0: {
                struct sockaddr sa;
@@ -229,7 +227,7 @@ static bool read_packet(vpn_packet_t *packet) {
        }
 
        case 1: {
-               if((inlen = read(request_fd, &request, sizeof(request))) != sizeof(request)) {
+               if(read(request_fd, &request, sizeof(request)) != sizeof(request)) {
                        logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading request from %s %s: %s", device_info,
                               device, strerror(errno));
                        event_exit();
@@ -264,7 +262,9 @@ static bool read_packet(vpn_packet_t *packet) {
        }
 
        case 2: {
-               if((inlen = read(data_fd, DATA(packet), MTU)) <= 0) {
+               ssize_t inlen = read(data_fd, DATA(packet), MTU);
+
+               if(inlen <= 0) {
                        logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
                               device, strerror(errno));
                        event_exit();
index 8463502..1c455e6 100644 (file)
@@ -34,8 +34,8 @@
 static const char *device_info = "raw_socket";
 
 static bool setup_device(void) {
-       struct ifreq ifr;
-       struct sockaddr_ll sa;
+       struct ifreq ifr = {0};
+       struct sockaddr_ll sa = {0};
 
        if(!get_config_string(lookup_config(&config_tree, "Interface"), &iface)) {
                iface = xstrdup("eth0");
@@ -51,8 +51,6 @@ static bool setup_device(void) {
                return false;
        }
 
-       memset(&ifr, 0, sizeof(ifr));
-
 #ifdef FD_CLOEXEC
        fcntl(device_fd, F_SETFD, FD_CLOEXEC);
 #endif
@@ -67,7 +65,6 @@ static bool setup_device(void) {
                return false;
        }
 
-       memset(&sa, '0', sizeof(sa));
        sa.sll_family = AF_PACKET;
        sa.sll_protocol = htons(ETH_P_ALL);
        sa.sll_ifindex = ifr.ifr_ifindex;
index a0a0281..5144b79 100644 (file)
@@ -1519,7 +1519,10 @@ static int cmd_pcap(int argc, char *argv[]) {
 static void sigint_handler(int sig) {
        (void)sig;
 
-       fprintf(stderr, "\n");
+       if(write(2, "\n", 1) < 0) {
+               // nothing we can do
+       }
+
        shutdown(fd, SHUT_RDWR);
 }
 #endif
@@ -1931,16 +1934,18 @@ static int cmd_config(int argc, char *argv[]) {
        char filename[PATH_MAX];
 
        if(node) {
-               if((size_t)snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, node) >= sizeof(filename)) {
-                       fprintf(stderr, "Filename too long: %s" SLASH "%s\n", hosts_dir, node);
-                       free(node);
-                       return 1;
-               }
+               size_t wrote = (size_t)snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, node);
 
                if(node != line) {
                        free(node);
                        node = NULL;
                }
+
+               if(wrote >= sizeof(filename)) {
+                       fprintf(stderr, "Filename too long: %s" SLASH "%s\n", hosts_dir, node);
+                       return 1;
+               }
+
        } else {
                snprintf(filename, sizeof(filename), "%s", tinc_conf);
        }
index 539f527..20d0e29 100644 (file)
@@ -185,6 +185,7 @@ static bool parse_options(int argc, char **argv) {
                        goto exit_fail;
 
                case OPT_CONFIG_FILE:
+                       assert(optarg);
                        free(confbase);
                        confbase = get_path_arg(optarg);
                        break;
@@ -216,6 +217,7 @@ static bool parse_options(int argc, char **argv) {
                        break;
 
                case OPT_NETNAME:
+                       assert(optarg);
                        free(netname);
                        netname = xstrdup(optarg);
                        break;
@@ -280,6 +282,7 @@ static bool parse_options(int argc, char **argv) {
                        break;
 
                case OPT_PIDFILE:
+                       assert(optarg);
                        free(pidfilename);
                        pidfilename = get_path_arg(optarg);
                        break;