From fa1f9502ffd5ca80bc6816ea18de1a2f6098d7e6 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 6 Feb 2024 10:11:47 +0100 Subject: [PATCH] main: do not leak sockfd on errors Signed-off-by: Giuseppe Scrivano --- main.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index f664ec4..28e2c23 100644 --- a/main.c +++ b/main.c @@ -165,7 +165,7 @@ static int configure_network(const char *tapname, .ifr_flags = IFF_UP | IFF_RUNNING }; if (ioctl(sockfd, SIOCSIFFLAGS, &ifr_lo) < 0) { perror("cannot set device up"); - return -1; + goto fail; } memset(&ifr, 0, sizeof(ifr)); @@ -174,20 +174,20 @@ static int configure_network(const char *tapname, if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) { perror("cannot set device up"); - return -1; + goto fail; } ifr.ifr_mtu = (int)cfg->mtu; if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) { perror("cannot set MTU"); - return -1; + goto fail; } if (cfg->vmacaddress_len > 0) { ifr.ifr_ifru.ifru_hwaddr = cfg->vmacaddress; if (ioctl(sockfd, SIOCSIFHWADDR, &ifr) < 0) { perror("cannot set MAC address"); - return -1; + goto fail; } } @@ -197,13 +197,13 @@ static int configure_network(const char *tapname, if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) { perror("cannot set device address"); - return -1; + goto fail; } sai->sin_addr = cfg->vnetmask; if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) { perror("cannot set device netmask"); - return -1; + goto fail; } memset(&route, 0, sizeof(route)); @@ -223,9 +223,12 @@ static int configure_network(const char *tapname, if (ioctl(sockfd, SIOCADDRT, &route) < 0) { perror("set route"); - return -1; + goto fail; } return 0; +fail: + close(sockfd); + return -1; } /* Child (--target-type=netns) */