Skip to content

Commit ac3bc6c

Browse files
Petr BaudisJunio C Hamano
Petr Baudis
authored and
Junio C Hamano
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c64ea85 commit ac3bc6c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

connect.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ static enum protocol get_protocol(const char *name)
328328
*/
329329
static int git_tcp_connect_sock(char *host)
330330
{
331-
int sockfd = -1;
331+
int sockfd = -1, saved_errno = 0;
332332
char *colon, *end;
333333
const char *port = STR(DEFAULT_GIT_PORT);
334334
struct addrinfo hints, *ai0, *ai;
@@ -362,9 +362,12 @@ static int git_tcp_connect_sock(char *host)
362362
for (ai0 = ai; ai; ai = ai->ai_next) {
363363
sockfd = socket(ai->ai_family,
364364
ai->ai_socktype, ai->ai_protocol);
365-
if (sockfd < 0)
365+
if (sockfd < 0) {
366+
saved_errno = errno;
366367
continue;
368+
}
367369
if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
370+
saved_errno = errno;
368371
close(sockfd);
369372
sockfd = -1;
370373
continue;
@@ -375,7 +378,7 @@ static int git_tcp_connect_sock(char *host)
375378
freeaddrinfo(ai0);
376379

377380
if (sockfd < 0)
378-
die("unable to connect a socket (%s)", strerror(errno));
381+
die("unable to connect a socket (%s)", strerror(saved_errno));
379382

380383
return sockfd;
381384
}
@@ -387,7 +390,7 @@ static int git_tcp_connect_sock(char *host)
387390
*/
388391
static int git_tcp_connect_sock(char *host)
389392
{
390-
int sockfd = -1;
393+
int sockfd = -1, saved_errno = 0;
391394
char *colon, *end;
392395
char *port = STR(DEFAULT_GIT_PORT), *ep;
393396
struct hostent *he;
@@ -426,15 +429,18 @@ static int git_tcp_connect_sock(char *host)
426429

427430
for (ap = he->h_addr_list; *ap; ap++) {
428431
sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
429-
if (sockfd < 0)
432+
if (sockfd < 0) {
433+
saved_errno = errno;
430434
continue;
435+
}
431436

432437
memset(&sa, 0, sizeof sa);
433438
sa.sin_family = he->h_addrtype;
434439
sa.sin_port = htons(nport);
435440
memcpy(&sa.sin_addr, *ap, he->h_length);
436441

437442
if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
443+
saved_errno = errno;
438444
close(sockfd);
439445
sockfd = -1;
440446
continue;
@@ -443,7 +449,7 @@ static int git_tcp_connect_sock(char *host)
443449
}
444450

445451
if (sockfd < 0)
446-
die("unable to connect a socket (%s)", strerror(errno));
452+
die("unable to connect a socket (%s)", strerror(saved_errno));
447453

448454
return sockfd;
449455
}

0 commit comments

Comments
 (0)