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 5304ab5ea3085f1f860ff4cacbd8bf845ec49156..57065701f995d378a113fd1c19c7bcb35cc655a7 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 76f262e2c390ae2a49266238805e9ce9886062bc..beda6c41c0e27b2008a7c6e36caca7eb4486011e 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 c059ca3c2ca31d562f684851c0df8e2e98789145..4c8597a66708142f5b4f54240520e84f422b4bfa 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 2c40ead099b5a24f4ee86e8b4037b40544821113..0abafa9dc2aa5ef6d1c592e854fde9fc3eec81c0 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 7fb16297ca7cc19b6fdfdee75475cd95ae046cf5..bcadae11f8a2d7b4643a1f1fde0da83726e3e8b1 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 15d32db2fefaefe5bce6030441dd6af5c9f602b8..75b20ec9ccb12d4ba7b05d8b222b95d0134b12df 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 3ec8e5715474014f5706c4978b8445ee4ba51a25..02f6afaf95f372a7012b4e164aac33e5271d9520 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 7244779ee438daa3da96b7570fb9cd838f9af211..2fdab2a66a12a723096f361ad37ca52a28055ead 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 38ebd6fa2fbf1787061d43c68781a47d6442f8e6..be609114bdd0fe4bdcfd9c01d1dd0f6a6df0c5b5 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 3e902100150103b5690707e3aed50233ed616b93..5149714d110bc5af40bdf5cbf14f98820f052a23 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