From 953f5b4231bbbb8269bb0c55b96a1c8c4bb34a59 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 6 Oct 2018 18:18:45 +0200 Subject: [PATCH] Fix warnings from the Clang static analyzer. --- configure.ac | 1 + src/control.c | 4 ++-- src/invitation.c | 4 +++- src/net_setup.c | 2 ++ src/tincctl.c | 13 ++++++++----- src/tincctl.h | 2 +- src/xalloc.h | 2 +- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index ca12640..3ad2821 100644 --- a/configure.ac +++ b/configure.ac @@ -181,6 +181,7 @@ AC_CHECK_HEADERS([netinet/tcp.h netinet/ip_icmp.h netinet/icmp6.h], dnl Checks for typedefs, structures, and compiler characteristics. tinc_ATTRIBUTE(__malloc__) +tinc_ATTRIBUTE(__nonnull__) tinc_ATTRIBUTE(__warn_unused_result__) AC_CHECK_TYPES([struct ether_header, struct arphdr, struct ether_arp, struct ip, struct icmp, struct ip6_hdr, struct icmp6_hdr, struct nd_neighbor_solicit, struct nd_opt_hdr], , , diff --git a/src/control.c b/src/control.c index beda6c4..71258b5 100644 --- a/src/control.c +++ b/src/control.c @@ -159,12 +159,12 @@ bool init_control(void) { // Get the address and port of the first listening socket char *localhost = NULL; - sockaddr_t sa; + sockaddr_t sa = {0}; socklen_t len = sizeof(sa); // Make sure we have a valid address, and map 0.0.0.0 and :: to 127.0.0.1 and ::1. - if(getsockname(listen_socket[0].tcp.fd, (struct sockaddr *)&sa, &len)) { + if(getsockname(listen_socket[0].tcp.fd, &sa.sa, &len)) { xasprintf(&localhost, "127.0.0.1 port %s", myport); } else { if(sa.sa.sa_family == AF_INET) { diff --git a/src/invitation.c b/src/invitation.c index 4c8597a..c3072cf 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -181,6 +181,7 @@ char *get_my_hostname() { if(!tty) { if(!hostname) { fprintf(stderr, "Could not determine the external address or hostname. Please set Address manually.\n"); + free(port); return NULL; } @@ -199,6 +200,7 @@ again: if(!fgets(line, sizeof(line), stdin)) { fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno)); free(hostname); + free(port); return NULL; } @@ -644,7 +646,7 @@ static char *grep(const char *data, const char *var) { } static bool finalize_join(void) { - char *name = xstrdup(get_value(data, "Name")); + const char *name = get_value(data, "Name"); if(!name) { fprintf(stderr, "No Name found in invitation!\n"); diff --git a/src/net_setup.c b/src/net_setup.c index d8b3116..d48c229 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -975,6 +975,7 @@ static bool setup_myself(void) { myself->incipher = NULL; } else if(!(myself->incipher = cipher_open_by_name(cipher))) { logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!"); + free(cipher); return false; } @@ -1002,6 +1003,7 @@ static bool setup_myself(void) { myself->indigest = NULL; } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) { logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!"); + free(digest); return false; } diff --git a/src/tincctl.c b/src/tincctl.c index 2fdab2a..4f6660a 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -2151,7 +2151,7 @@ static bool try_bind(int port) { return success; } -int check_port(char *name) { +int check_port(const char *name) { if(try_bind(655)) { return 655; } @@ -3175,10 +3175,7 @@ static int cmd_shell(int argc, char *argv[]) { free(line); rl_basic_word_break_characters = "\t\n "; line = readline(prompt); - - if(line) { - copy = xstrdup(line); - } + copy = line ? xstrdup(line) : NULL; } else { line = fgets(buf, sizeof(buf), stdin); } @@ -3224,6 +3221,9 @@ static int cmd_shell(int argc, char *argv[]) { } if(!strcasecmp(nargv[argc], "exit") || !strcasecmp(nargv[argc], "quit")) { +#ifdef HAVE_READLINE + free(copy); +#endif free(nargv); return result; } @@ -3252,6 +3252,9 @@ static int cmd_shell(int argc, char *argv[]) { } } +#ifdef HAVE_READLINE + free(copy); +#endif free(nargv); if(tty) { diff --git a/src/tincctl.h b/src/tincctl.h index db7f45a..3f7d003 100644 --- a/src/tincctl.h +++ b/src/tincctl.h @@ -48,7 +48,7 @@ extern char *get_my_name(bool verbose); extern bool connect_tincd(bool verbose); extern bool sendline(int fd, char *format, ...); extern bool recvline(int fd, char *line, size_t len); -extern int check_port(char *name); +extern int check_port(const char *name); extern FILE *fopenmask(const char *filename, const char *mode, mode_t perms); extern ecdsa_t *get_pubkey(FILE *f); diff --git a/src/xalloc.h b/src/xalloc.h index 493af5c..ac24ebc 100644 --- a/src/xalloc.h +++ b/src/xalloc.h @@ -53,7 +53,7 @@ static inline void *xrealloc(void *p, size_t n) { return p; } -static inline char *xstrdup(const char *s) __attribute__((__malloc__)); +static inline char *xstrdup(const char *s) __attribute__((__malloc__, __nonnull__)); static inline char *xstrdup(const char *s) { char *p = strdup(s); -- 2.25.1