diff mbox series

[OpenWrt-Devel,firewall3] utils: coverity resource leak warning

Message ID 20190612155315.35735-1-ldir@darbyshire-bryant.me.uk
State Accepted
Delegated to: Kevin Darbyshire-Bryant
Headers show
Series [OpenWrt-Devel,firewall3] utils: coverity resource leak warning | expand

Commit Message

Kevin 'ldir' Darbyshire-Bryant June 12, 2019, 3:53 p.m. UTC
solve coverity reported resource leak (socket handle)

Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
---
 utils.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

Comments

Jo-Philipp Wich June 12, 2019, 3:56 p.m. UTC | #1
Hi Kevin,

ACK. Feel free to push.

~ Jo
diff mbox series

Patch

diff --git a/utils.c b/utils.c
index 7f09787..fc6bbd7 100644
--- a/utils.c
+++ b/utils.c
@@ -944,18 +944,24 @@  bool
 fw3_check_loopback_dev(const char *name)
 {
 	struct ifreq ifr;
-	int s = socket(AF_LOCAL, SOCK_DGRAM, 0);
+	int s;
 	bool rv = false;
 
+	s = socket(AF_LOCAL, SOCK_DGRAM, 0);
+
+	if (s < 0)
+		return false;
+
 	memset(&ifr, 0, sizeof(ifr));
 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
 
-	if (s < 0 || ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
-		goto out;
+	if (ioctl(s, SIOCGIFFLAGS, &ifr) >= 0) {
+		if (ifr.ifr_flags & IFF_LOOPBACK)
+			rv = true;
+	}
+
+	close(s);
 
-	if (ifr.ifr_flags & IFF_LOOPBACK)
-		rv = true;
-out:
 	return rv;
 }