From: Kirill Isakov Date: Mon, 23 May 2022 19:22:38 +0000 (+0600) Subject: Enable more clang-tidy checks X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=c5373d9fc0c8e3dfb6529b35bf44efe58410e0c7;p=tinc Enable more clang-tidy checks --- diff --git a/.clang-tidy b/.clang-tidy index d0a89c18..d204cff4 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -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: "*" diff --git a/src/fsck.c b/src/fsck.c index 70ca84d6..b44b7759 100644 --- a/src/fsck.c +++ b/src/fsck.c @@ -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); diff --git a/src/invitation.c b/src/invitation.c index 5db1e987..151f701b 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -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"); diff --git a/src/linux/uml_device.c b/src/linux/uml_device.c index dafe5b20..2004850f 100644 --- a/src/linux/uml_device.c +++ b/src/linux/uml_device.c @@ -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(); diff --git a/src/raw_socket_device.c b/src/raw_socket_device.c index 8463502d..1c455e6a 100644 --- a/src/raw_socket_device.c +++ b/src/raw_socket_device.c @@ -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; diff --git a/src/tincctl.c b/src/tincctl.c index a0a02810..5144b793 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -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); } diff --git a/src/tincd.c b/src/tincd.c index 539f5274..20d0e297 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -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;