Fix issues found by Coverity.
[oweals/tinc.git] / src / linux / device.c
index d418aaf..b4360a6 100644 (file)
@@ -107,17 +107,21 @@ static bool setup_device(void) {
                ifr.ifr_flags |= IFF_ONE_QUEUE;
 #endif
 
-       if(iface)
+       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);
-               if(iface) free(iface);
+               ifrname[IFNAMSIZ - 1] = 0;
+               free(iface);
                iface = xstrdup(ifrname);
        } else if(!ioctl(device_fd, (('T' << 8) | 202), &ifr)) {
                logger(LOG_WARNING, "Old ioctl() request was needed for %s", device);
                strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
-               if(iface) free(iface);
+               ifrname[IFNAMSIZ - 1] = 0;
+               free(iface);
                iface = xstrdup(ifrname);
        } else
 #endif
@@ -126,8 +130,7 @@ static bool setup_device(void) {
                        overwrite_mac = true;
                device_info = "Linux ethertap device";
                device_type = DEVICE_TYPE_ETHERTAP;
-               if(iface)
-                       free(iface);
+               free(iface);
                iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
        }