Fix compiler warnings.
authorGuus Sliepen <guus@tinc-vpn.org>
Sat, 6 Oct 2018 15:51:41 +0000 (17:51 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sat, 6 Oct 2018 15:51:41 +0000 (17:51 +0200)
src/conf.c
src/control.c
src/invitation.c
src/linux/device.c
src/logger.c
src/net_socket.c
src/raw_socket_device.c
src/tincctl.c
src/uml_device.c
src/upnp.c

index 5304ab5..5706570 100644 (file)
@@ -433,7 +433,11 @@ bool read_server_config(void) {
 
                                // And we try to read the ones that end with ".conf"
                                if(l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
-                                       snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name);
+                                       if(snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name) >= sizeof(fname)) {
+                                               logger(DEBUG_ALWAYS, LOG_ERR, "Pathname too long: %s/%s", dname, ep->d_name);
+                                               return false;
+                                       }
+
                                        x = read_config_file(config_tree, fname, true);
                                }
                        }
index 76f262e..beda6c4 100644 (file)
@@ -201,6 +201,8 @@ bool init_control(void) {
 
        strncpy(sa_un.sun_path, unixsocketname, sizeof(sa_un.sun_path));
 
+       sa_un.sun_path[sizeof(sa_un.sun_path) - 1] = 0;
+
        if(connect(unix_fd, (struct sockaddr *)&sa_un, sizeof(sa_un)) >= 0) {
                logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
                return false;
index c059ca3..4c8597a 100644 (file)
@@ -353,7 +353,11 @@ int cmd_invite(int argc, char *argv[]) {
 
                char invname[PATH_MAX];
                struct stat st;
-               snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name);
+
+               if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name) >= sizeof(invname)) {
+                       fprintf(stderr, "Filename too long: %s" SLASH "%s\n", filename, ent->d_name);
+                       continue;
+               }
 
                if(!stat(invname, &st)) {
                        if(deadline < st.st_mtime) {
@@ -955,7 +959,11 @@ ask_netname:
                line[strlen(line) - 1] = 0;
 
                char newbase[PATH_MAX];
-               snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line);
+
+               if(snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line) >= sizeof(newbase)) {
+                       fprintf(stderr, "Filename too long: " CONFDIR SLASH "tinc" SLASH "%s\n", line);
+                       goto ask_netname;
+               }
 
                if(rename(confbase, newbase)) {
                        fprintf(stderr, "Error trying to rename %s to %s: %s\n", confbase, newbase, strerror(errno));
index 2c40ead..0abafa9 100644 (file)
@@ -103,10 +103,12 @@ static bool setup_device(void) {
 
        if(iface) {
                strncpy(ifr.ifr_name, iface, IFNAMSIZ);
+               ifr.ifr_name[IFNAMSIZ - 1] = 0;
        }
 
        if(!ioctl(device_fd, TUNSETIFF, &ifr)) {
                strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
+               ifrname[IFNAMSIZ - 1] = 0;
                free(iface);
                iface = xstrdup(ifrname);
        } else {
index 7fb1629..bcadae1 100644 (file)
@@ -186,7 +186,7 @@ void openlogger(const char *ident, logmode_t mode) {
                loghandle = RegisterEventSource(NULL, logident);
 
                if(!loghandle) {
-                       fprintf(stderr, "Could not open log handle!");
+                       fprintf(stderr, "Could not open log handle!\n");
                        logmode = LOGMODE_NULL;
                }
 
index 15d32db..75b20ec 100644 (file)
@@ -210,6 +210,7 @@ int setup_listen_socket(const sockaddr_t *sa) {
 
                memset(&ifr, 0, sizeof(ifr));
                strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
+               ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
 
                if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) {
                        closesocket(nfd);
index 3ec8e57..02f6afa 100644 (file)
@@ -60,6 +60,7 @@ static bool setup_device(void) {
 #endif
 
        strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
+       ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
 
        if(ioctl(device_fd, SIOCGIFINDEX, &ifr)) {
                close(device_fd);
index 7244779..2fdab2a 100644 (file)
@@ -355,6 +355,8 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo
        char buf[PATH_MAX];
        char buf2[PATH_MAX];
 
+ask_filename:
+
        /* Check stdin and stdout */
        if(ask && tty) {
                /* Ask for a file and/or directory name. */
@@ -385,7 +387,17 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo
 #endif
                /* The directory is a relative path or a filename. */
                getcwd(directory, sizeof(directory));
-               snprintf(buf2, sizeof(buf2), "%s" SLASH "%s", directory, filename);
+
+               if(snprintf(buf2, sizeof(buf2), "%s" SLASH "%s", directory, filename) >= sizeof(buf2)) {
+                       fprintf(stderr, "Filename too long: %s" SLASH "%s\n", directory, filename);
+
+                       if(ask && tty) {
+                               goto ask_filename;
+                       } else {
+                               return NULL;
+                       }
+               }
+
                filename = buf2;
        }
 
@@ -825,6 +837,8 @@ bool connect_tincd(bool verbose) {
 
        strncpy(sa.sun_path, unixsocketname, sizeof(sa.sun_path));
 
+       sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
+
        fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
        if(fd < 0) {
@@ -1161,7 +1175,12 @@ static int dump_invitations(void) {
                }
 
                char fname[PATH_MAX];
-               snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ent->d_name);
+
+               if(snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ent->d_name) >= sizeof(fname)) {
+                       fprintf(stderr, "Filename too long: %s" SLASH "%s\n", dname, ent->d_name);
+                       continue;
+               }
+
                FILE *f = fopen(fname, "r");
 
                if(!f) {
@@ -1930,7 +1949,11 @@ static int cmd_config(int argc, char *argv[]) {
        FILE *tf = NULL;
 
        if(action >= -1) {
-               snprintf(tmpfile, sizeof(tmpfile), "%s.config.tmp", filename);
+               if(snprintf(tmpfile, sizeof(tmpfile), "%s.config.tmp", filename) >= sizeof(tmpfile)) {
+                       fprintf(stderr, "Filename too long: %s.config.tmp\n", filename);
+                       return 1;
+               }
+
                tf = fopen(tmpfile, "w");
 
                if(!tf) {
@@ -2537,7 +2560,10 @@ static int cmd_import(int argc, char *argv[]) {
                                fclose(out);
                        }
 
-                       snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, name);
+                       if(snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, name) >= sizeof(filename)) {
+                               fprintf(stderr, "Filename too long: %s" SLASH "%s\n", hosts_dir, name);
+                               return 1;
+                       }
 
                        if(!force && !access(filename, F_OK)) {
                                fprintf(stderr, "Host configuration file %s already exists, skipping.\n", filename);
@@ -3264,7 +3290,7 @@ int main(int argc, char *argv[]) {
        static struct WSAData wsa_state;
 
        if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
-               fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
+               fprintf(stderr, "System call `%s' failed: %s\n", "WSAStartup", winerror(GetLastError()));
                return false;
        }
 
index 38ebd6f..be60911 100644 (file)
@@ -133,6 +133,7 @@ static bool setup_device(void) {
 
        listen_sun.sun_family = AF_UNIX;
        strncpy(listen_sun.sun_path, device, sizeof(listen_sun.sun_path));
+       listen_sun.sun_path[sizeof(listen_sun.sun_path) - 1] = 0;
 
        if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof(listen_sun)) < 0) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
index 3e90210..5149714 100644 (file)
@@ -61,7 +61,7 @@ static struct UPNPDev *upnp_discover(int delay, int *error) {
 
 #else
 
-#if MINIUPNPC_API_VERSION > 15
+#if MINIUPNPC_API_VERSION > 17
 #warning "The version of libminiupnpc you're building against seems to be too recent. Expect trouble."
 #endif