From 7a74b815fac370d431e0987272e7474804108b11 Mon Sep 17 00:00:00 2001 From: Ruslan Kuchumov Date: Sat, 9 Aug 2014 21:23:27 +0000 Subject: [PATCH] segfault on error fix --- src/networking.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/networking.c b/src/networking.c index 6979658b..1d8a1342 100644 --- a/src/networking.c +++ b/src/networking.c @@ -570,21 +570,25 @@ int tcp_bind(const char *port, int *family) } } - if (0 == bind(sockfd, p->ai_addr, p->ai_addrlen)) - break; - + if (bind(sockfd, p->ai_addr, p->ai_addrlen) < 0) + { #if _WIN32 - wprintf(L"bind() eror: %ld\n", WSAGetLastError()); - closesocket(sockfd); + wprintf(L"bind() eror: %ld\n", WSAGetLastError()); + closesocket(sockfd); #else - mprint("bind() error: %s\n", strerror(errno)); - close(sockfd); + mprint("bind() error: %s\n", strerror(errno)); + close(sockfd); #endif - if (p->ai_next != NULL) - mprint("trying next addres ...\n"); - } + if (p->ai_next != NULL) + mprint("trying next addres ...\n"); - *family = p->ai_family; + continue; + } + + *family = p->ai_family; + + break; + } freeaddrinfo(ai);