[gnutls-devel] [PATCH] fixed socket existance checking

Marcus Meissner meissner at suse.de
Wed Feb 12 20:58:43 CET 2014


>From 970008c7706852b4703e7a2bed149fc94e897211 Mon Sep 17 00:00:00 2001
From: Marcus Meissner <meissner at suse.de>
Date: Wed, 12 Feb 2014 20:56:17 +0100
Subject: [PATCH] fixed socket existance checking

If getaddrinfo returns: ipv4 address, ipv6 address ... and socket() for
the ipv6 address fails, this loop would fail and abort the socket listen
code.
---
 src/serv.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/serv.c b/src/serv.c
index 60d4e97..c3e33ae 100644
--- a/src/serv.c
+++ b/src/serv.c
@@ -712,7 +712,7 @@ int listen_socket(const char *name, int listen_port, int socktype)
 {
 	struct addrinfo hints, *res, *ptr;
 	char portname[6];
-	int s;
+	int s = -1;
 	int yes;
 	listener_item *j = NULL;
 
@@ -732,6 +732,7 @@ int listen_socket(const char *name, int listen_port, int socktype)
 	}
 
 	for (ptr = res; ptr != NULL; ptr = ptr->ai_next) {
+		int news;
 #ifndef HAVE_IPV6
 		if (ptr->ai_family != AF_INET)
 			continue;
@@ -747,11 +748,12 @@ int listen_socket(const char *name, int listen_port, int socktype)
 						 sizeof(topbuf)));
 		}
 
-		if ((s = socket(ptr->ai_family, ptr->ai_socktype,
+		if ((news = socket(ptr->ai_family, ptr->ai_socktype,
 				ptr->ai_protocol)) < 0) {
 			perror("socket() failed");
 			continue;
 		}
+		s = news; /* to not overwrite existing s from previous loops */
 #if defined(HAVE_IPV6) && !defined(_WIN32)
 		if (ptr->ai_family == AF_INET6) {
 			yes = 1;
-- 
1.8.1.4




More information about the Gnutls-devel mailing list