Fix errno usage in connect.c
errno was used after it could've been modified by a subsequent library call. Spotted by Morten Welinder. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>
Petr Baudis committed
Jul 1, 2006 at 23:56 UTC
ac3bc6c1d1f549f6809d8a7d29bcaed302f3c193
1 file changed
+12
-6
connect.c
+12
-6
@@ -328,7 +328,7 @@ static enum protocol get_protocol(const char *name)
328
*/
329
static int git_tcp_connect_sock(char *host)
330
{
331
- int sockfd = -1;
331
+ int sockfd = -1, saved_errno = 0;
332
char *colon, *end;
333
const char *port = STR(DEFAULT_GIT_PORT);
334
struct addrinfo hints, *ai0, *ai;
@@ -362,9 +362,12 @@ static int git_tcp_connect_sock(char *host)
362
for (ai0 = ai; ai; ai = ai->ai_next) {
363
sockfd = socket(ai->ai_family,
364
ai->ai_socktype, ai->ai_protocol);
365
- if (sockfd < 0)
365
+ if (sockfd < 0) {
366
+ saved_errno = errno;
367
continue;
368
+ }
369
if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
370
+ saved_errno = errno;
371
close(sockfd);
372
sockfd = -1;
373
continue;
@@ -375,7 +378,7 @@ static int git_tcp_connect_sock(char *host)
378
freeaddrinfo(ai0);
379
380
if (sockfd < 0)
378
- die("unable to connect a socket (%s)", strerror(errno));
381
+ die("unable to connect a socket (%s)", strerror(saved_errno));
382
383
return sockfd;
384
}
@@ -387,7 +390,7 @@ static int git_tcp_connect_sock(char *host)
390
*/
391
static int git_tcp_connect_sock(char *host)
392
{
390
- int sockfd = -1;
393
+ int sockfd = -1, saved_errno = 0;
394
char *colon, *end;
395
char *port = STR(DEFAULT_GIT_PORT), *ep;
396
struct hostent *he;
@@ -426,8 +429,10 @@ static int git_tcp_connect_sock(char *host)
429
430
for (ap = he->h_addr_list; *ap; ap++) {
431
sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
429
- if (sockfd < 0)
432
+ if (sockfd < 0) {
433
+ saved_errno = errno;
434
continue;
435
+ }
436
437
memset(&sa, 0, sizeof sa);
438
sa.sin_family = he->h_addrtype;
@@ -435,6 +440,7 @@ static int git_tcp_connect_sock(char *host)
440
memcpy(&sa.sin_addr, *ap, he->h_length);
441
442
if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
443
+ saved_errno = errno;
444
close(sockfd);
445
sockfd = -1;
446
continue;
@@ -443,7 +449,7 @@ static int git_tcp_connect_sock(char *host)
449
}
450
451
if (sockfd < 0)
446
- die("unable to connect a socket (%s)", strerror(errno));
452
+ die("unable to connect a socket (%s)", strerror(saved_errno));
453
454
return sockfd;
455
}