diff mbox series

[rpld,5/6] socket: make sure we close our socket if setsockopt() fails

Message ID 20190918195819.7492-6-stefan@datenfreihafen.org (mailing list archive)
State Not Applicable
Headers show
Series Mixed bag of rpld patches | expand

Commit Message

Stefan Schmidt Sept. 18, 2019, 7:58 p.m. UTC
If any of our setsockopt() calls fails we returned a -1 but
kept the socket available.

Fixes Coverity ID 250804
---
 socket.c | 7 +++++++
 1 file changed, 7 insertions(+)
diff mbox series

Patch

diff --git a/socket.c b/socket.c
index 866b544..e6bbb52 100644
--- a/socket.c
+++ b/socket.c
@@ -92,30 +92,35 @@  int open_icmpv6_socket(const struct list_head *ifaces)
 	err = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, (int[]){1}, sizeof(int));
 	if (err < 0) {
 		flog(LOG_ERR, "setsockopt(IPV6_RECVPKTINFO): %s", strerror(errno));
+		close(sock);
 		return -1;
 	}
 
 	err = setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, (int[]){2}, sizeof(int));
 	if (err < 0) {
 		flog(LOG_ERR, "setsockopt(IPV6_CHECKSUM): %s", strerror(errno));
+		close(sock);
 		return -1;
 	}
 
 	err = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (int[]){255}, sizeof(int));
 	if (err < 0) {
 		flog(LOG_ERR, "setsockopt(IPV6_UNICAST_HOPS): %s", strerror(errno));
+		close(sock);
 		return -1;
 	}
 
 	err = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (int[]){1}, sizeof(int));
 	if (err < 0) {
 		flog(LOG_ERR, "setsockopt(IPV6_MULTICAST_HOPS): %s", strerror(errno));
+		close(sock);
 		return -1;
 	}
 
 	err = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (int[]){0}, sizeof(int));
 	if (err < 0) {
 		flog(LOG_ERR, "setsockopt(IPV6_MULTICAST_LOOP): %s", strerror(errno));
+		close(sock);
 		return -1;
 	}
 
@@ -123,6 +128,7 @@  int open_icmpv6_socket(const struct list_head *ifaces)
 	err = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, (int[]){1}, sizeof(int));
 	if (err < 0) {
 		flog(LOG_ERR, "setsockopt(IPV6_RECVHOPLIMIT): %s", strerror(errno));
+		close(sock);
 		return -1;
 	}
 #endif
@@ -130,6 +136,7 @@  int open_icmpv6_socket(const struct list_head *ifaces)
 	err = subscribe_rpl_multicast(sock, ifaces);
 	if (err < 0) {
 		flog(LOG_ERR, "Failed to subscribe rpl multicast: %s", strerror(errno));
+		close(sock);
 		return -1;
 	}